• Post Reply Bookmark Topic Watch Topic
  • New Topic

String.matches(range) does not work with range with double digits  RSS feed

 
Surinder Mehra
Ranch Hand
Posts: 44
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to match the input string with range of values and getting PatternSyntaxException. below is my code




output: true

But when i try to give range in double digits, it throws exception. What if i have to match a number(as string) between 10 to 25 or like that.





Output:

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal character range near index 5
0[14-16]
^
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regex character sets don't work like that see http://www.regular-expressions.info/charclass.html.
You can only specify the digits 0-9 you can't specify anything outside this range as you aren't matching numeric values you are matching numeric characters. To match any two digit numbers you can use [0-9]{2}
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See http://www.regular-expressions.info/numericranges.html

You'll need something like [1-9][1-9] for 2 digit numbers.
 
Surinder Mehra
Ranch Hand
Posts: 44
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for quick help !
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!