• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.util.regex.PatternSyntax Exception  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am getting java.util.regex.PatternSyntaxException: Illegal repetition in my code .Here is the code

import java.util.regex.*;

class Regular
{
public static void main(String[] args)
{
// Create a pattern to match cat
String s=" <a href='https://info.plan.csplans.com/pdfs/forms/${ClientId}/${PlanId}/.DOC_SPD.pdf'/></a> Click Here ";
String client=getReg(s,"${ClientId}","abc");
String plan=getReg(client,"${PlanId}","5678");
System.out.println(plan);

}

private static String getReg(String input,String ptrn,String replace){

Pattern p = Pattern.compile(ptrn);

// Create a matcher with an input string
Matcher m = p.matcher(input);
StringBuffer sb = new StringBuffer();
boolean result = m.find();
//create a new String
// with the replacements
if(result) {
m.appendReplacement(sb, replace);
//result = m.find();
}
// Add the last segment of input to
// the new String
m.appendTail(sb);
//System.out.println(sb.toString());
return sb.toString();

}

}

Thanks,
Basu
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Escape your $, { and }. In Java, everything special meaning chars have to be escaped unlike javascript or perl.


String client = getReg(s, "\\$\\{ClientId\\}", "abc");
String plan = getReg(client, "\\$\\{PlanId\\}", "5678");
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Lim:
Escape your $, { and }. In Java, everything special meaning chars have to be escaped unlike javascript or perl.


If I remember correctly, in Perl regexes you have to escape special characters too, don't you?
[ August 29, 2005: Message edited by: Ilja Preuss ]
 
Choon-Chern Lim
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Urrr... yeah... you are right. I wasn't thinking correctly.

But in Java, we always have to "escape" the escape character, so "\" becomes "\\" and "\\" becomes "\\\\".
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Lim:
Urrr... yeah... you are right. I wasn't thinking correctly.

But in Java, we always have to "escape" the escape character, so "\" becomes "\\" and "\\" becomes "\\\\".


Well "always" in String literals, yes. Can actually get quite enyoing in combination with regexes. Try to write a regex that replaces a backslash by a double-backslash
 
Choon-Chern Lim
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah.... java regexps with backlashes are always a pain. I mean, I kind of lose count how many slashes I need until I get some compilation errors.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Lim:
Yeah.... java regexps with backlashes are always a pain. I mean, I kind of lose count how many slashes I need until I get some compilation errors.


And once it compiles, I need unit tests to tell me that the code really is doing what I want it to do...
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!