Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems with replaceAll()

 
S. Lohi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to convert a match-string to a format that my java program could understand.

Lets say the string is str = "foo*bar*".
I try to convert it to java format using str.replaceAll("*", ".*").
Shouldn't be a big deal, I figure, but unfortunately when the program tries to execute that line of code hell breaks loose. I get a dozen Errors and Exceptions, starting with

java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0
*
^

I know that happens when the string is like "*foo*bar" (starting with "*"), but why does it happen in this case?

Any help would be appreciated.

-SLohi
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"*" is a special character apparently. You might need to escape it with the traditional "\".
 
S. Lohi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tried that, gives an "invalid escape sequence".
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use double \ like "\\*"
 
S. Lohi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that was... simple ( ).

Thank you very much.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When figuring out regular expression patterns, as quick references I like to have available the Pattern class documentation, and the two introductory articles I wrote on the java.util.regex package, which can be found listed at http://www.javaranch.com/journal.jsp (search for regex).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic