• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular expression exception URGENT please..  RSS feed

 
bhargavi kakarala
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • 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.
 
Joseph Sillitoe
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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
  • 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}\"
^
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Alan .It worked.....
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!