• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular Expression Help  RSS feed

 
An Sush
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a template xml, with value of nodes substitued as
<name>${name}</name>
I parsed this template xml in java and want to replace all occurances of ${name} based on some business logic.

When I use this in my java class:
Pattern p = Pattern.compile("${name}");
Matcher m = p.matcher(strTemplate); // strTemplate has d template XML parsed
strReturn = m.replaceAll("John");


I am getting the follwing error:
java.util.regex.PatternSyntaxException: Illegal repetition near index 0
${name}


Can you help me in replacing the string ${name} with say John
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that the chars $, {, and } all have special significance to the regex compiler (in the Pattern.compile() method) and so they need to be escaped. Try

or equivalently (since in this case you want to treat all the chars as literal):

[ June 01, 2007: Message edited by: Jim Yingst ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This duplicates Another Thread over in Performance. I responded there.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose this thread focused on the regex syntax question, while the Performance question asked more about performance. Still, I'll close this one so we don't waste further time duplicating efforts. Sushant, please ask each question in one forum only. Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!