MarkLoveExEx
asked on
Java Regular Expression
I have a File f, and the following code:
String name = f.getName().replaceAll("(? i)\\.gz$", "");
What exactly is being matched and replaced?
String name = f.getName().replaceAll("(?
What exactly is being matched and replaced?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. mccarl -- You had the better answer. Dan -- Thanks for the input. It was helpful, but mccarl did point out an error.
You're welcome!
Looks like Java was very late to the regex party and already used \ as an escape character in the literal strings: http://www.regular-expressions.info/java.html
Makes for some funny looking expressions...
Makes for some funny looking expressions...
something + \ + any character + gz.
Use these options for the whole regular expression «(?i)»
&Case insensitive «i»
Match the backslash character «\\»
Match any single character that is NOT a line break character (line feed, carriage return, next line, line separator, paragraph separator) «.»
Match the character string “gz” literally (case insensitive) «gz»
Assert position at the end of the string, or before the line break at the end of the string, if any (carriage return and line feed, next line, line separator, paragraph separator) «$»