Regex to match filename containing a word, regardless of case

23,385

Solution 1

Like @Sebastian P mentioned:

/^.*icon.*\.png$/i

Except I'm adding the i flag to the end to mark it as case-insensitive.

Solution 2

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.

See also:

Share:
23,385
tzippy
Author by

tzippy

Updated on July 05, 2022

Comments

  • tzippy
    tzippy almost 2 years

    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

  • Sebastian Paaske Tørholm
    Sebastian Paaske Tørholm about 13 years
    It would, but then C:\icons\panda.png would match, too.
  • tchrist
    tchrist about 13 years
    @Sebaastin: So? That was the requirement.
  • Sebastian Paaske Tørholm
    Sebastian Paaske Tørholm about 13 years
    @tchrist: If you re-read the requirements, it should match any filename that contains icon and ends in .png. If the path is C:\icons\panda.png, the filename does not contain icon, the path does.
  • tchrist
    tchrist about 13 years
    @Sebastian: If the open(2) syscall accepts it, it is clearly a filename. The presence or absense of directory separators is a matter to be left to namei(9) to resolve for you, and has no bearing on whether you have a legal filename or not. That means that "/" is just as much of a filename as "." or ".." or "......." or "foo" or "foo/foo" or "/foo/foo/foo.foo.foo/../foofoo/..foo" is.
  • Brad Christie
    Brad Christie about 13 years
    @SebastianP, Though I'm sure the extra-mile is appreciated, there was no indication the original input would include a filepath, only a filename. ;-)
  • tchrist
    tchrist about 13 years
    @Brad: "filename", "pathname", and "path" are wholly synonymous. See your local kernel. A "file" is of course different from a "filename", being related to the underlying inode rather than the path to reach it. But there is no sense of "file path" that is different from "filename" or "pathname".
  • Sebastian Paaske Tørholm
    Sebastian Paaske Tørholm about 13 years
    @tchrist: Now you're nitpicking. When people say filename, they usually mean the name given to a file. Most people wouldn't consider moving a file to another directory renaming the file. That said, if that's what he desires, he can just use the first regex, as it works for that case, as well.
  • tzippy
    tzippy about 13 years
    I have to agree with Sebastian. Thanks for the input and the discussion. But if I had the same definition for "filename" like you, @tchrist I would not ask a question about such a simple regex ;)
  • tchrist
    tchrist about 13 years
    @Sebastian: To rename => rename - change the name of a file on BSD, Solaris, and MacOS. All those accept rename("/abc/xyz","/def/xyz") which changes its path. To change its path is to change its name. Filename and pathname have no separate meaning. Perhaps you are confusing basename. Not system programmers, eh?
  • tchrist
    tchrist about 13 years
    @Sebastian: Your pattern fails on filenames with any of the Java newline sequences in them. Mine doesn’t.