简体   繁体   English

正则表达式匹配文件名

[英]Regex matching file names

I have some template for file names like: 我有一些文件名模板,如:

$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

order of type , sample-id , subject-id not constantly set. type顺序, sample-idsubject-id不经常设置。

I need to select files which matching this templates. 我需要选择与此模板匹配的文件。

for example: 例如:

for first template maching string: 对于第一个模板加工字符串:

"123A-subject-info.xml" ($subject-id$ is string)

for second: 第二个:

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

for last: 最后:

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

How can i do it with regex ? 我怎么能用正则表达式做到这一点? I try to parsing this strings using split method, but i need to rewrite this method constantly if the template was changed. 我尝试使用split方法解析此字符串,但如果模板已更改,我需要不断重写此方法。

For example if the file is in php then it would be /^\\S[a-zA-Z]+.(php)$/ But you can replace php with your preferred extension. 例如,如果文件是在php中,那么它将是/ ^\\S[a-zA-Z]+ .(php) $/但是你可以用你喜欢的扩展名替换php。

try using http://regex101.com/ to check your regular expression 尝试使用http://regex101.com/检查您的正则表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM