首页 > 解决方案 > C# Regex - match a substring in a filename

问题描述

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.

标签: c#regex

解决方案


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


推荐阅读