我有一些文件名模板,如:
$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方法解析此字符串,但如果模板已更改,我需要不断重写此方法.
小智 1
例如,如果文件是 php 格式,那么它将是 /^\S[a-zA-Z]+.(php)$/ 但您可以将 php 替换为您喜欢的扩展名。
尝试使用http://regex101.com/检查您的正则表达式