Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex matching file names

Tags:

c#

regex

parsing

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.

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.

like image 423
al__gol Avatar asked Nov 11 '22 09:11

al__gol


1 Answers

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.

try using http://regex101.com/ to check your regular expression

like image 86
Sudarshan Subramanian Avatar answered Nov 15 '22 08:11

Sudarshan Subramanian