Home > Software engineering >  RegEx: how to don't match a repetition
RegEx: how to don't match a repetition

Time:12-21

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 )

  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

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 or A, b or B, c or C, then one or more digits and then zero or more lowercase ASCII letters.
  • Related