0

My regex is really poor so I need help with a c# regex expression that can match a substring after the last backslash.

Typical input:

D:\DataFiles\Files_81\aars2016FAKH1800010.pdf

I need to check if the filename aars2016FAKH1A800010.pdf contains "FAKH1". It is important that only the filename is evaluated.

It must be done with C# regex, so please no "Contains"

You might be wondering why regex, but this is going to be used in a generic c# application that can evaluate regex expressions. Thank you in advance.

D-Shih
  • 44,943
  • 6
  • 31
  • 51
Peter Forsbom
  • 89
  • 1
  • 10
  • 1
    This _shouldn't_ be done with Regex, it should be done with `FileInfo` or the `Path` static methods. The framework already has support for this, you do not need the overhead and maintainability costs that come with Regex – maccettura Sep 24 '18 at 18:04

1 Answers1

0

You can try to use \\\w*(FAKH)\w*\.pdf pattern.

bool isExsit = Regex.IsMatch(@"D:\DataFiles\Files_81\aars2016FAKH1800010.pdf", @"\\\w*(FAKH)\w*\.pdf");

EDIT

You can use Groups[1].Value get FAKH

var result = Regex.Match(@"D:\DataFiles\Files_81\aars2016FAKH1800010.pdf", @"\\\w*(FAKH)\w*\.pdf");
var FAKH = result.Groups[1].Value;

c# online

D-Shih
  • 44,943
  • 6
  • 31
  • 51
  • But is it possible to just return the word it self not the entire filename. \\\w*(FAKH)\w*\.pdf returns aars2016FAKH1800010.pdf. I would like to just return FAKH – Peter Forsbom Sep 25 '18 at 06:25
  • @PeterForsbom edit my answer – D-Shih Sep 25 '18 at 06:51
  • But I do not have access to the C# code. I have an application written in C#. This application evaluates file paths. In this application I can enter regex expressions. – Peter Forsbom Sep 25 '18 at 07:08
  • I can just enter: (FAKH) but that will also get hits on the file path. I need to just evaluate everything after the last "\" – Peter Forsbom Sep 25 '18 at 07:10
  • @PeterForsbom You need to use regex group in c# otherwise you can't get `FAKH`. How about use the `\\\w*(FAKH)\w*\`.pdf` if that return value we can know the result is `FAKH` – D-Shih Sep 25 '18 at 07:15