简体   繁体   中英

Filename regex extraction

I'd like to get parts of the filename

filename blabla_2009-001_name_surname-name_surname

I'd like to get: 2009-001, name_surname, name_surname

I've come up with this, but it's no good

preg_match("/(?:[0-9-]{8})_(?:[a-z_]+)-(?:[a-z_]+)/", $filename, $matches);

Any pointers?

Thanks! BR

preg_match("([0-9-]{8})_([^_]+)_([^_]+)-([^_]+)_([^_]+)$", $filename, $matches);

Assuming filename format doesn't change:

preg_match('#(\d{4}-\d{3})_(.*?)-(.*?)$#', $filename, $match);

Updated version to handle extension:

preg_match('#(\d{4}-\d{3})_(.*?)-(.*?)\.(.*?)$#', $filename, $match);

if your file name is always that structure

$name = "blabla_2009-001_name_surname-name_surname";
$s = explode("_",$name,3);
$t = explode("-",end($s));
print "$s[1] $t[0] $t[1]\n";

output

$ php test.php
2009-001 name_surname name_surname

Thanks you all,

I've updated my own solution As "nikc" pointed out...i just needed some direction...

here's the code if anyone will need a similar solution...

Thank you all!

preg_match('/([0-9-]{8})_([^-]+)-([a-z_]+).([a-z]+)/', $filename, $matches);

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