guest788
guest788

Reputation: 31

RegEx: how to don't match a repetition

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

Answers (2)

CinCout
CinCout

Reputation: 9619

Use the following regex:

^.*?([aA][bB][cC]\d+)

  1. Use ^ to begin at the start of the input
  2. .*? matches zero or more characters (except line breaks) as few times as possible (lazy approach)
  3. The rest is then captured in the capturing group as expected.

Demo

Upvotes: 3

Wiktor Stribiżew
Wiktor Stribiżew

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

Related Questions