• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why doesn't this match?

 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



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

Henry
 
Darrin Smith
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic