• 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

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

 
Ranch Hand
Posts: 44
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
^
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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}
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for quick help !
 
reply
    Bookmark Topic Watch Topic
  • New Topic