aspose file tools*
The moose likes Java in General and the fly likes Why doesn't this match? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why doesn Watch "Why doesn New topic
Author

Why doesn't this match?

Darrin Smith
Ranch Hand

Joined: Aug 04, 2003
Posts: 276
It's been a while since I have done much regex work so I am sure it's something simple, but I cannot get this thing to match when several regular expression checkers say it should!

Here is the code

public static final Pattern pattern = Pattern.compile(".*=([0-9|.]*),([0-9|.]*),([0-9|.]*),([0-9|.]*),([0-9|.]*),([0-9|.]*),([0-9|.]*),([0-9|.]*)");
...

Matcher m = pattern.matcher("GN=12.7,0,0.9,12.9,0.0,1,1,0");

if(m.matches())
{
//NEVER happens. m.matches() always returns false, but why?
}

Why does m.matches() return false?

Thanks!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7677
    
  19

Darrin Smith wrote:Why does m.matches() return false?

Well, I suspect you don't need the '|' because you don't appear to be matching on it, but I wouldn't have thought it would need escaping inside square brackets.
I also think that "(-?[0-9]+([.][0-9]+)?)" is more correct for a decimal number.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18748
    
  40

Darrin Smith wrote:
Why does m.matches() return false?


When I tested it, I got "true" from m.matches().

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Darrin Smith
Ranch Hand

Joined: Aug 04, 2003
Posts: 276
Henry Wong wrote:
Darrin Smith wrote:
Why does m.matches() return false?


When I tested it, I got "true" from m.matches().

Henry


Every time I run the code it shows false. Driving me up a wall trying to figure it out. It SHOULD match.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18748
    
  40

Darrin Smith wrote:
Every time I run the code it shows false. Driving me up a wall trying to figure it out. It SHOULD match.


Then there is something that you are not telling us... because the code that you provided DOES result in a match.

Here's my code...



Henry
Darrin Smith
Ranch Hand

Joined: Aug 04, 2003
Posts: 276
Winston Gutkowski wrote:
Darrin Smith wrote:Why does m.matches() return false?

Well, I suspect you don't need the '|' because you don't appear to be matching on it, but I wouldn't have thought it would need escaping inside square brackets.
I also think that "(-?[0-9]+([.][0-9]+)?)" is more correct for a decimal number.

Winston


I tried doing this too...no help:

public static final Pattern pattern = Pattern.compile(".*=(-?[0-9]+\\.[0-9]+?),([0-9]+),(-?[0-9]+\\.[0-9]+?),(-?[0-9]+\\.[0-9]+?),(-?[0-9]+\\.[0-9]+?),([0-9]*),([0-9]*),([0-9]*)");


Again though, using a tool it works. Just not when I run it in a Java application.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7677
    
  19

Darrin Smith wrote:I tried doing this too...no help:
...
Pattern.compile(".*=(-?[0-9]+\\.[0-9]+?),([0-9]+),(-?[0-9]+\\.[0-9]+?),(-?[0-9]+\\.[0-9]+?),(-?[0-9]+\\.[0-9]+?),([0-9]*),([0-9]*),([0-9]*)");
...
Again though, using a tool it works. Just not when I run it in a Java application.

That's not quite what I was thinking of. I tried this:
Pattern.compile("GN=-?[0-9]+([.][0-9]+)?(,-?[0-9]+([.][0-9]+)?){7}");
against your test string and it worked just fine.

I suspect you should also make your initial ".*" string ".*?" or "[^=]*".

Winston

Just FYI, my code:
and the output:

Darrin Smith
Ranch Hand

Joined: Aug 04, 2003
Posts: 276
I tried that and it did work on the string I sent but failed on others (sometimes no decimal was sent, etc.). I modified it slightly and ended up with this which works great!

private static final Pattern getStatusPattern = Pattern.compile("GN=(-?[0-9]+\\.?[0-9]?),(-?[0-9]+\\.?[0-9]?),(-?[0-9]+\\.?[0-9]?),(-?[0-9]+\\.?[0-9]?),(-?[0-9]+\\.?[0-9]?),(-?[0-9]+\\.?[0-9]?),(-?[0-9]+\\.?[0-9]?)");

Thanks!



Winston Gutkowski wrote:
Darrin Smith wrote:I tried doing this too...no help:
...
Pattern.compile(".*=(-?[0-9]+\\.[0-9]+?),([0-9]+),(-?[0-9]+\\.[0-9]+?),(-?[0-9]+\\.[0-9]+?),(-?[0-9]+\\.[0-9]+?),([0-9]*),([0-9]*),([0-9]*)");
...
Again though, using a tool it works. Just not when I run it in a Java application.

That's not quite what I was thinking of. I tried this:
Pattern.compile("GN=-?[0-9]+([.][0-9]+)?(,-?[0-9]+([.][0-9]+)?){7}");
against your test string and it worked just fine.

I suspect you should also make your initial ".*" string ".*?" or "[^=]*".

Winston

Just FYI, my code:
and the output:

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7677
    
  19

Darrin Smith wrote:I tried that and it did work on the string I sent but failed on others (sometimes no decimal was sent, etc.).

Ah. That's why, when asking questions about regexes, it's very important to specify ALL of the requirements.

I modified it slightly and ended up with this which works great!

Great. Two things I can see though:

1. Your "number" pattern will allow a number with a decimal point but no decimal digits, eg: "12.".

2. You can reduce the pattern size a lot by providing a 'number of' qualifier, as in my example above, viz:
"GN=(-?[0-9]+\\.?[0-9]?)(,-?[0-9]+\\.?[0-9]?){6}"
as opposed to
"GN=(-?[0-9]+\\.?[0-9]?),(-?[0-9]+\\.?[0-9]?),(-?[0-9]+\\.?[0-9]?),(-?[0-9]+\\.?[0-9]?),(-?[0-9]+\\.?[0-9]?),(-?[0-9]+\\.?[0-9]?),(-?[0-9]+\\.?[0-9]?)"

If the comma must always be present (as it would seem from your example) you could use:
"GN=(-?[0-9]+\\.?[0-9]?)(,(-?[0-9]+\\.?[0-9]?)?){6}"
Which will match any of "GN=12.7,,,,,,", "GN=12.7,0,0.9,,,," or "GN=12.7,0,0.9,12.9,0.0,1,1"

You can even make it a range. For example:
"GN=(-?[0-9]+\\.?[0-9]?)(,-?[0-9]+\\.?[0-9]?){0,6}"
Would match any of "GN=12.7", "GN=12.7,0,0.9" or "GN=12.7,0,0.9,12.9,0.0,1,1"

In fact, the "?" (optional) qualifier can actually be written as "{0,1}", although you don't see it too often.

HIH

Winston
 
 
subject: Why doesn't this match?