Regex optional character with a conditional clause

I have a regex problem that combines the ideas of optional characters and conditional regex statements that I’m unsure how to solve.

I want to find a pattern that, in addition to matching an initial number, will also match the following uppercase letter if and only if that character is not followed by a lowercase letter. The string will only ever have one number. For example:

'fds;o2Ko ' ==> '2'
'rejy 3ked' ==> '3'
's.fg6G hb' ==> '6G'
'3M- gfafg' ==> '3M'
'dgfAN adg' ==> no pattern found

I’ve tried various combinations of conditional statements but can’t seem to combine the concepts properly. I’m working in python using the following code:

pattern = r'[1-9][A-Z]?' 
ID = str(re.findall(pattern, 's.fg6G hb')).strip('[]'')

The above is what I want without the conditional statement. I’m unsure how to include an appropriate conditional statement. I think pattern would be something like r'[1-9](?(?=[A-Z][a-z])[A-Z]?|)' but don’t understand how I can look ahead beyond the current character.

Answer

It seems you can try to use:

d+(?:[A-Z](?![a-z]))?

See the online demo

  • d+ – 1+ Digits.
  • (?: – Open non-capture group:
    • [A-Z] – A single uppercase alpha.
    • (?![a-z]) – A nested negative lookahead to assert position is not followed by a lowercase alpha.
    • )? – Close non-capture group and make it optional.