c# - 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.
解决方案
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;
推荐阅读
- nasm - 如何删除旧的 nasm 构建并安装新的?
- html - Vidoes 不能在 HTTPS 上播放带有 Angular 的旧 Safari
- java - ANDROID - 如何将 URL 与字符串进行比较
- handlebars.js - Handlebars:如何将 `rel=preload` 参数添加到 `{{stylesheet}}` 标签?
- css - 如何通过悬停其他元素使元素可见
- mongodb - 用于添加两列的 Mongo 模板标准查询
- excel - 在 VBA 中返回具有早期绑定的字典的函数
- flutter - Flutter 中的 NoSuchMethod 异常
- python - 使用 Python3 将文本包装在 .txt 文件中?
- javascript - 如果使用javascript的两个数组的empId相同,如何删除重复的empId记录行