File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Using * in Regular Expressions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using * in Regular Expressions" Watch "Using * in Regular Expressions" New topic

Using * in Regular Expressions

Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2591

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

Joined: Sep 22, 2001
Posts: 2591

Forget it!
I got it! It should have been "\\*" or "\\Q*\\E", so it's working now!
- Manish
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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.
I agree. Here's the link:
subject: Using * in Regular Expressions
It's not a secret anymore!