I’m currently working on a .NET 4.6.2 application.
I need to write a regex to filter certain files.
The filename must not include the word "house" as well as the file suffix png, jpg, or gif.
So far I came up with this regex:
Regex regex = new Regex(@"\b\w*house\w*\b.+.(jpg|png|gif)$");
It seems to work fine with the following words:
But it doesn’t filter these words i.e.:
Do you know how to write a regex to solve this issue?
The pattern does not match the last 2 strings because
.+ matches 1 or more characters and the
. after it also matches a character.
So after matching
house there should be 2 of any characters after it, and then match any of the alternatives
Depending on the allowed characters, you could match 0 or more characters followed by escaping the dot to match it literally.
If you don’t need to capture the suffix, you can wrap the alternatives in a non capture group:
Or you could narrow down the allowed characters matching only word chars and a hyphen and start the pattern matching word chars without a word boundary: