• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

RegEx Pattern with ? -exception

 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All,

I have below code...


And my properties file has




Am getting

Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '?' near index 10
IN|AP|HYD|?(.*)|BILLS


Whats wrong with the pattern

Rgds
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The pattern in the error message is not the same as the pattern in the properties file. That's a clue. I believe a backslash is a special character in properties files in the same way that it is in string literals. So the pattern is interpreted as shown in the error message, and the ? is therefore in an invalid position.

Try using a double backslash instead, which will be unescaped to a single backslash and should do what you want.
 
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

A Kumar wrote:my properties file has
Am getting
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '?' near index 10
IN|AP|HYD|?(.*)|BILLS
Whats wrong with the pattern


Basically, the '?' doesn't have anything to work with.

'?' means "0 or 1", so it needs a subpattern to work with. The previous character to it is a '|' though, which is ALSO a metacharacter, so the pattern compiler is telling you that it doesn't have anything to evaluate.

HIH

Winston
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic