简体   繁体   中英

File sequence > Find Name Pattern

I'm trying to figure out a solid way to solve multiple types of file sequences.

Consider these sequences

file_0000.jpg
file_0001.jpg
file_0002.jpg etc
&
new1File001.jpg
new1File002.jpg
new1File003.jpg

So it needs to find out where the first decimal of the sequence code starts.

FileInfo[] files = new DirectoryInfo(@"\\fileserver\").GetFiles("*.*", SearchOption.AllDirectories);
var grouped = files.OrderBy(f => f.Name).GroupBy(f => f.Name.Substring(0, f.Name.LastIndexOf("_")));

Obviously this finds file sequences where the sequence numbering is separated by "_". I want it to be filtered by the position of the first decimal of the last decimal sequence. My regex skills are not good and even then I don't know how to use it in the lamba expression.

The main question is, how can I find out where the number string starts for the above mentioned cases.

Any pointers would be great!
Thanks,
-Johan

Yes, regex is to rescue:

var r = new Regex(@".+(\d{2,}).");
var grouped = 
    files.
        OrderBy(f => f.Name).
        GroupBy(f => r.Match(f.Name).Groups[0].Value);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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