Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex to filter word with suffix from string

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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:

  • zt_housedsaf-34.png
  • housedsaf-34.gif

But it doesn’t filter these words i.e.:

  • house.gif
  • 123house.png

Do you know how to write a regex to solve this issue?

>Solution :

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 jpg png gif.

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:

\b\w*house\w*\b.*\.(?:jpg|png|gif)$

Regex demo

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:

\w*house[\w-]*\.(?:jpg|png|gif)$

Regex demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading