Search code examples
javaregexregex-group

Get extension without dot using regex


I have the regex (.*)(\.zip|\.tar\.gz) meaning I have two groups, the name ( first part) and the second one that's the extension.

I want to be able to get the second group either zip, tar, or tar.gz without the ., only the extension name, how I can do that using regex only, I am using Java and I don't want to do any substring.


Solution

  • Move the dot outside the second capturing group.

    (.*)\.(zip|tar(?:\.gz)?)