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?
MarkLoveExExAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan CraciunIT ConsultantCommented:
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) «$»
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
@Dan,

It strips the extension for all files terminated with:
something + \ + any character + gz.

That's not quite right. Because we are talking about Java here the result is a little different. The double backslash \\ is a Java escape to represent a normal single backslash. Then that single backslash is a Regex escape to indicate the following period . is a literal and shouldn't considered as the "any character" wildcard.

So essentially it matches (case-insensitively) any filename that ends with    .gz   and then replaces that   .gz   with "" (nothing), ie. it strips the     .gz    off the end of any filename that may have it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MarkLoveExExAuthor Commented:
Thanks.  mccarl -- You had the better answer. Dan -- Thanks for the input. It was helpful, but mccarl did point out an error.
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
You're welcome!
0
Dan CraciunIT ConsultantCommented:
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...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.