I need a regex that matches any filename of the type .png containing the word icon in all its cases.
So it should match
icon.png
myicon.png
thisIcon.PnG
aniCon_this.png
ANYICON.PNG
[email protected]
Any help appreciated!! Thanks! PS: I'm in java
Like @Sebastian P mentioned:
/^.*icon.*\.png$/i
Except I'm adding the i flag to the end to mark it as case-insensitive.
If you only need to verify filenames, this should do the trick:
Pattern regex = Pattern.compile("^.*icon.*\\.png$", Pattern.CASE_INSENSITIVE);
If you get paths also, and want to extract the filename, use this:
Pattern regex = Pattern.compile("(?<=^|[\\\\/])([^\\\\/]*icon[^\\\\/]*\\.png)$", Pattern.CASE_INSENSITIVE);
To explain this one: I use negated character classes for \ and / to ensure that everything is part of the filename, and then I ensure that we go until the start of the filename with a lookbehind.
Pattern classIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With