• 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

Regular expression exception URGENT please..

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

I have a regular expression in XML tag as

<spec>"{?i}impt{?-i}.*\.{?i}abc{?-i}"</spec>

when I try to compile the pattern as follows
public void addFilePattern(String a, String spec)
{
Pattern ptn = Pattern.compile(spec);


I am getting this exception "java.util.regex.PatternSyntaxException: Illegal repetition near index 0"

could anyone of you please tell me how torectify it.I even tried ^ and $ at the starting and ending of the expression instead of ".

thanks in advance.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need the quotes at all.

just try:



Unless, of course, the quote is part of the pattern that you are checking against. In that case, I think you would need to escape them with a \ like this:

 
bhargavi kakarala
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joseph,
I tried that already.

<spec>\"{?i}impt{?-i}.*\.{?i}abc{?-i}\"</spec>

java.util.regex.PatternSyntaxException: Illegal repetition near index 1
\"{?i}impt{?-i}.*\.{?i}abc{?-i}\"
^
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You only need to escape quotes when you're creating the regex as a String literal. The problem here is with the inline modifier syntax--it uses parentheses, not braces:You could also write it like this:In fact, since the middle part doesn't contain any letters, you can just use this:
 
bhargavi kakarala
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Alan .It worked.....
 
Getting married means "We're in love, so let's tell the police!" - and invite this tiny ad to the wedding:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic