Bart Van Den Boomen Bart Van Den Boomen - 4 days ago 6
C# Question

Regex in c# to find files in log

I'm trying to find some filenames that are written into a logfile that end on 'K.TIF'.

I'm trying to find:

20130629VGM180ZZ001001K.TIF
20130629VGM180ZZ001002K.TIF


etc.

As I'm terrible in regex's, I tried this:

Regex.Match(line, @"([A-Z0-9]+){23}\.TIF", RegexOptions.IgnoreCase);
Regex.Match(line, @"(?<=\\)(.>)(?=K\.TIF){23}", RegexOptions.IgnoreCase);


The first one is terrible, doesn't perform and gives bad results.

The second one actually gives all the TIF that end on Z.TIF if I change K\ to Z. However, it does not find any K.TIF's with the current regex.

Answer

This seems to work for me:

^.*\\(\w*K.TIF)$

It searches for the last slash and then captures the word characters followed by K.TIF. Example: http://www.regex101.com/r/nH6gV4

Comments