Home > Software engineering >  Regex, how can I extract everything after a character only if it is followed by a set of numbers tha
Regex, how can I extract everything after a character only if it is followed by a set of numbers tha

Time:02-24

How can I write a regex that will allow me to extract everything after “V” but only if it is followed by a set of numbers that can also include letters? There are also instances where there is a “V” but I do not want anything to be extracted.

Examples:

Input                       Desired Output
V123456789                  123456789
V1TZ234567                  1TZ234567
02032022 RIVER CC           (blank)

CodePudding user response:

This seems to satisfy your requirements:

V(?=\d)([A-Z0-9] )

It matches V, then uses a positive lookahead to make sure there is a digit, and then capture any uppercase letter or digit that comes after V.

CodePudding user response:

regnew=re.findall(r'V([0-9] [A-Z0-9] )',out)
print(regnew)
 output.
['123456789', '1TZ234567']
  • Related