Regex matches one part of a string

I have these types of strings:

aabbcc-12-00001-g001-1024.png

ddeeffgg-1-50001-i001-500.jpg

ddeeffgg-1-50001-i001.jpg

I would like to extract the file name without its size, which will always be -xxxeither -xxxxbefore the file extension.

For example, for three lines, I would like to match:

aabbcc-12-00001-g001.png

ddeeffgg-1-50001-i001.jpg

ddeeffgg-1-50001-i001.jpg

My idea was to mark the size first and then use preg_replace with PHP, but not sure how to fit the size as soon as I try:

-\d{3,4}\.[a-z]{3}

But this corresponds to -550.png or -1024.jpg, etc.

+4
source share
1 answer

You can use preg_replace with replacement:

$str = 'aabbcc-12-00001-g001-1024.png';
$result = preg_replace('/(-\d+)(.[A-Z]+)$/i', '$2', $str);
echo $result;
+5
source

Source: https://habr.com/ru/post/1696157/


All Articles