Home > Net >  Regex Capturing alternating letters and numbers only when it does not begin the string
Regex Capturing alternating letters and numbers only when it does not begin the string

Time:10-08

I'm trying to capture alternating numbers and alphabets (alphabets come first) and ultimately remove them, unless it starts the string.

So in the below example, yellow is what I'm trying to capture:

enter image description here

While I'm identifying the correct rows I'm having a hard time just capturing just the yellow highlighted however...

^(?!([A-Z] \d \w*))(?:(. ))[A-Z] \d \w*

https://regexr.com/673hl

Any help greatly appreciated.

CodePudding user response:

You can use

(?!^)\b[A-Z] \d \w*

See the regex demo. Details:

  • (?!^) - a negative lookahead that matches a position that is NOT at the start of string
  • \b - match a word boundary, the preceding char must a non-word char (or start of string, but the lookahead above already ruled that position out)
  • [A-Z] - one or more uppercase ASCII letters
  • \d - one or more digits
  • \w* - zero or more letters, digits or underscores.

If you want to match any kind of alphanumeric strings add an alternative:

(?!^)\b(?:[A-Z] \d|\d [A-Z])\w*

And to make it case insensitive:

(?!^)\b(?:[A-Za-z] \d|\d [A-Za-z])\w*
  • Related