Reputation: 31
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.
Upvotes: 1
Views: 69
Reputation: 9619
Use the following regex:
^.*?([aA][bB][cC]\d+)
^
to begin at the start of the input.*?
matches zero or more characters (except line breaks) as few times as possible (lazy approach)Upvotes: 3
Reputation: 626689
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
or A
, b
or B
, c
or C
, then one or more digits and then zero or more lowercase ASCII letters.Upvotes: 3