This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regular expression

 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can I escape ".", "-", "_" in a regular expression?

when I do:



in a jsp I get:

Generated servlet error:
Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joseph Sweet:
how can I escape ".", "-", "_" in a regular expression?


You have to remember that regular expressions are held in Java Strings in Java -- which IMO, makes it annoying to use.

So to escape ".", it is indeed "\."... but "\" has special meaning to a Java string, so you must escape that in order to pass it to the regex engine.... so it should be "\\.".

Henry
 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic