• Post Reply Bookmark Topic Watch Topic
  • New Topic

REGEX Help  RSS feed

 
David Garratt
Ranch Hand
Posts: 216
Eclipse IDE Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a problem getting a regex expression to validate an string.

My Expression looks like this :-
[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-5-[1]][0-5-[1234]]

My String looks like this
0059093310

I was expecting the "1" to fail the match as I had a expression [0-5-[1]] which I thought meant numbers 0 through 5 excluding 1 would be acceptable.

In summary I want the expression to accept numbers 0-9 in positions 1 to 8 in the string.

Position 9 can be 0,2,3,4,5 (excluding 1)

Position 10 can be 0 or 5

The code I am using to test this looks like this :-


public static boolean isStringPatternValid(String regex, String input)
{
boolean result = true;

Pattern pat = Pattern.compile(regex);

Matcher mat = pat.matcher(input);

result = mat.matches();

return result;

}

Any help much appreciated. Thanks.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, how about


Or if you want to exclude 1 from the 9th position, you use ^ not -:
 
David Garratt
Ranch Hand
Posts: 216
Eclipse IDE Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So my 1st attempt was pretty rubbish.

I don't understand why the 1st line you posted didn't exclude 1 in position 9.

String regex = "\\d{8}[02-5][05]";

I looks like it should

Can you explain why the absence of 1 in [02-5] does not do the trick

Thanks again

Dave
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That line does indeed exclude the "1" from position 9; it just doesn't use explicit exclusion (via the "^" operator), but does it implicitly by not listing "1" as one of the allowed characters.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

\\d{8} : 0-9 in first 8 slots
[02-5] : 0, 2, 3, 4, 5 in next slot
[05] : 0, 5 in last slot


[0-5&&[^1]] : 0-5 except 1 (same as 0, 2, 3, 4, 5 above)
 
David Garratt
Ranch Hand
Posts: 216
Eclipse IDE Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that my code must be wrong then as I'm not getting back the results I want.

From my 1st post I'm passing a regex and an input string to a routine and just wanting to get back a true false as to if it complies with the rule.

As you can tell I've not used this before so I'm out of my comfort zone.

Dave

Not sure if it's relevant but my code uses these library imports

import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
David Garratt
Ranch Hand
Posts: 216
Eclipse IDE Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok scratch the last post - I was a muppet :-)

It does work.

Many thanks for everyones help

Dave
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't [0-5-[1234]] (if that syntax would be allowed) not simply (0|5) or [05]?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!