![](/img/trans.png)
[英]C# - Regex - Matching file names according to a specific naming pattern
[英]Regex matching file names
我有一些文件名模板,如:
$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
type
順序, sample-id
, subject-id
不經常設置。
我需要選擇與此模板匹配的文件。
例如:
對於第一個模板加工字符串:
"123A-subject-info.xml" ($subject-id$ is string)
第二個:
"2014-04-17-17-42-57-O1CD-sample-info.txt" ($sample-id$ is date time type)
最后:
"image-2014-04-17-17-42-57-01CD-mini.jpg" ($type$ is another string)
我怎么能用正則表達式做到這一點? 我嘗試使用split方法解析此字符串,但如果模板已更改,我需要不斷重寫此方法。
例如,如果文件是在php中,那么它將是/ ^\\S[a-zA-Z]+ .(php) $/但是你可以用你喜歡的擴展名替換php。
嘗試使用http://regex101.com/檢查您的正則表達式
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.