74233

Verifying that a Regular Expression Matches

Question:

I have a regex that, according to what I can tell and to what <a href="http://regexpal.com/" rel="nofollow">RegexPal</a> says, does not match the full string I am testing (only its rightmost part). However, matcher.matches() returns true!

So, what is the most reliable way to determine whether a java.util.regex Matcher actually fully matches a string?

Also, suppose that one wants to use matcher.find() as follows:

if (a match was found) { while (matcher.find()) { // Do whatever } ]

Is there any way of implementing the "a match was found" condition check?

Answer1:

matches() returning true would mean there some match. Whether it's the "full" string or not, simply depends on what your regex is. E.g.

"a"

would match all of the following

"a" "abb" "bab" "bba"

if you're looking to match full string, your regex must begin with ^ and end with $ E.g.

"^a$"

would match "a", but none of the following

"abb" "bab" "bba"

Answer2:

Don't use find, use matches.

Answer3:

Well, I've never had matches() not work, but you can use find(), then use

matcher.start()==0&&matcher.end()==string.length()

I don't think you need the if, because the while(matcher.find()) should check , but if you do...

if(matcher.find()){ do{ //whatever } while(matcher.find()); }

Recommend

  • Is there a way to specify an Ivy dependency using a dynamic revision but restricting the status?
  • Are there any possible ways to ignore all paths of JPA Example Matcher
  • Regex to match/group repeating characters in a string
  • Rounding returned float values from a DB to their 'correct' values
  • How to check whether a command can be executed?
  • Assignment of Allocatables of Different Shapes in Fortran [duplicate]
  • How can i match particular format in input using java.util.regex in java?
  • How to determine the CCSID used in CPYFRMIMPF command?
  • Should I be afraid to use UDP to make a client/server broadcast talk?
  • How to discover Font Type?
  • Android Database Error - getWriteableDatabase
  • Using Generics on right hand side in Java 6?
  • Hide HTML elements without javascript, only CSS
  • What is the purpose of TaskExecutor in spring?
  • how to find common suffix in java by using method
  • How to have background script and something similar to a default popup?
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • print() is showing quotation marks in results
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Android fill_parent issue
  • Extracting HTML between tags
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • MongoDB in PHP using aggregate to group by _id is null not working
  • How to rebase a series of branches?
  • Regex thinks I'm nesting, but I'm not
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Get object from AWS S3 as a stream
  • Cross-Platform Protobuf Serialization
  • Fill an image in a square container while keeping aspect ratio
  • Rearranging Cells in UITableView Bug & Saving Changes
  • How to format a variable of double type
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • coudnt use logback because of log4j
  • sending mail using smtp is too slow
  • JaxB to read class hierarchy
  • Django query for large number of relationships
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Reading document lines to the user (python)
  • How can I use `wmic` in a Windows PE script?