I have followings String:
test_abc123_firstrow
test_abc1564_secondrow
test_abc123_abc234_thirdrow
test_abc1663_fourthrow
test_abc193_abc123_fifthrow
I want to get the abc following number of each row. But just the first one if it has more than one.
My current pattern looks like this: ([aA][bB][cC]\w\d [a-z]*)
But this doesn't involve the first one only.
If somebody could help how I can implement that, that would be great.
CodePudding user response:
Use the following regex:
^.*?([aA][bB][cC]\d )
- Use
^
to begin at the start of the input .*?
matches zero or more characters (except line breaks) as few times as possible (lazy approach)- The rest is then captured in the capturing group as expected.
CodePudding user response:
You can use
^.*?([aA][bB][cC]\d [a-z]*)
Note the removed \w
, it matches letters, digits and underscores, so it looks redundant in your pattern.
The ^.*?
added at the start matches the
^
- start of string.*?
- any zero or more chars other than line break chars as few as possible([aA][bB][cC]\d [a-z]*)
- Capturing group 1:a
orA
,b
orB
,c
orC
, then one or more digits and then zero or more lowercase ASCII letters.