• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular Expresssions

 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope I'm in the right forum? Can someone help me understand why this keeps giving me the following error message:

Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )

java.util.regex.Pattern p = null;
p = java.util.regex.Pattern.compile( "\[\\+|\\-]?\d*\\.{1}\\d{2}[\\+|\\-]?" );
java.util.regex.Matcher m = p.matcher( "-123.99" );
System.out.println( m.matches() );

//THIS WORKS FINE...
p = java.util.regex.Pattern.compile( ""\\d*\\.{1}\\d{2}[\\+|\\-]?"" );
m = p.matcher( "123.99-" );
System.out.println( m.matches() );

I need the regular expression to accept either one of these values:
-123.99 or 123.99-. How can I modify the regular expression to accept either one of these values?

Thanks for all your help!
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks to me like you cut-and-pasted the [\\+|\\-]? string and split the double backslash in front of the d.

Try:
[\\+|\\-]?\\d*\\.{1}\\d{2}[\\+|\\-]?
 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joel, that worked! I thought all regular expressions had to start with the "\".
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!