• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

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

 
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]
^
 
Bartender
Posts: 3323
86
  • 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}
 
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 !
 
Catch Ernie! Catch the egg! And catch this tiny ad too:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!