This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using * in Regular Expressions

 
Manish Hatwalne
Ranch Hand
Posts: 2595
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do use the character * in RegEx, given that * is special Meta Character in RegEx. In other words how do I get following to run and print -
Manish Hatwalne?

BTW, I have tried "\*" and it doesn't work. It must be simple, but somehow I am unable to find it at the moment; and not too familiar with RegEx.
- Manish
[ April 26, 2004: Message edited by: Manish Hatwalne ]
 
Manish Hatwalne
Ranch Hand
Posts: 2595
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget it!
I got it! It should have been "\\*" or "\\Q*\\E", so it's working now!
- Manish
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Manish Hatwalne:
It should have been "\\*" or "\\Q*\\E", so it's working now!

In case someone comes along and wonders why that worked ...
The "\\*" is first seen by the compiler, which uses "\" to escape special characters just like regular expressions. It interprets "\\" as "\", making the String that actually gets stuffed into your .class file "\*". Then this String is sent to the regex parser, which uses the "\" to escape the "*".
Double-escaping is quite common and can get very crazy when you have languages that can interpret dynamic code created by the program itself. I remember doing a little work at c|net where they had built their own template engine. It was not uncommon to see things like
exec("print(''''hello world'''')");
Absolutely insane as strings were built and passed around to be executed again and again, requiring *exactly* the correct level of nesting/escaping of quotes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic