Link to home
Start Free TrialLog in
Avatar of MarkLoveExEx
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?
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Does it work? It strips the extension for all files terminated with:
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) «$»
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MarkLoveExEx
MarkLoveExEx

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...