Regex Match Different Optional Groups

I have been fiddeling for hours, but with no result… I am trying to match different optional groups in a string. The idea is to read out file-names and separate out the actual name from the extension, plus also an own string like ("_myString"). The pattern should only recognize the dot closest to the line end, but no other dot in the middle. The file names can look like this:

clip 0041.rtf.srt EN.srt_myString.rtf
clip 0041.rtf EN.txt
clip 0041.rtf.srt EN

I thought this search pattern should work…

(.*)(_myString)?(\.[a-zA-Z]*$)?

…and give out the following:

1 $1: clip 0041.rtf.srt EN.srt
1 $2: _myString
1 $3: .rtf

2 $1: clip 0041.rtf EN
2 $:2 [no match]
2 $3: .txt

3 $1: clip 0041.rtf.srt EN
3 $:2 [no match]
3 $:3 [no match]

But that is not the case. I get:

1 $1: clip 0041.rtf.srt EN.srt_myString.rtf
2 $1: [zero-width match]

and so on…
any ideas?

Try this:

(?U)^.+(_mystring)?(\.\w+)?$

Kem, you are my hero! Thank you so much.

1 Like