小编al_*_*gol的帖子

正则表达式匹配文件名

我有一些文件名模板,如:

$subject-id$-subject-info.xml
$sampleId$-$subjectId$-sample-info.txt
$subjectId$-$sampleId$-sample-info.txt
$subjectId$-$sampleId$-image-$type$.png
$sampleId$-$subjectId$-image-$type$.jpg
image-$sampleId$-$subjectId$-$type$.jpg
Run Code Online (Sandbox Code Playgroud)

秩序type,sample-id,subject-id不经常设置.

我需要选择与此模板匹配的文件.

例如:

对于第一个模板加工字符串:

"123A-subject-info.xml" ($subject-id$ is string)
Run Code Online (Sandbox Code Playgroud)

第二个:

"2014-04-17-17-42-57-O1CD-sample-info.txt" ($sample-id$ is date time type)
Run Code Online (Sandbox Code Playgroud)

最后:

"image-2014-04-17-17-42-57-01CD-mini.jpg" ($type$ is another string)
Run Code Online (Sandbox Code Playgroud)

我怎么能用正则表达式做到这一点?我尝试使用split方法解析此字符串,但如果模板已更改,我需要不断重写此方法.

c# regex parsing

5
推荐指数
1
解决办法
115
查看次数

标签 统计

c# ×1

parsing ×1

regex ×1