Regular expression question


#1

Hello,

I have file names such as:

WT 1.lsm
WT 2.lsm
WT 2 b.lsm
MUT 1.lsm

And I only want to extract WT or MUT.

In theory I think this should work?!

    ^(?P<Treatment>.*\s).*\s?.*.lsm

The idea of the (\s?) is that there might (or might not) be another space, following the first one.
But it stil gives WT 2 instead of WT for the WT 2 b.lsm filename.
Could you tell me the correct syntax?


#2

This works:
^(?P<Treatment>[A-Z]{2,3})\s.*.lsm

I tend to use regex101 to play with the different regex, but certainly there are other tools out there.

Good luck!


#3

Thank you very much. This works!