1

I am using the following expression in regex101 with PERL marked up. It works. But it does not work in python with re.

pattern = [0-9]{1,4}+(?![\.\/\-][A-Za-z]{2,})[A-Za-z]

What I am looking for is to catch numbers followed by a letter but NOT numbers followed by more than a letter.

Explanation:

  • [0-9]{1,4}+ digits up to 4 with a possessive quantifier
  • (?![./-][A-Za-z]{2,})negative look ahead: Which are not followed by 2 or more letters
  • [A-Za-z] a letter

The result is as follows: enter image description here

All fine, but this is marking up PERL in the regex101 website, as soon as I mark python: enter image description here

The possessive + symbol is not recognize.

One option is to resort to the library regex: https://pypi.org/project/regex/

but this is an suboptimal solution since re is part of the standard package.

Is there anyway to reproduce this + behavior in normal python regex (re)?

thanks

EDIT: To be clear, this other regex DOES NOT SOLVE THE PROBLEM:

pattern2="[0-9]{1,4}[A-Za-z](?![A-Za-z]{2,})"

because it catches a number and the first letter, what I want if that the regex gives me nothing if the number is followed by more than one letter.

JFerro
  • 3,203
  • 7
  • 35
  • 88
  • The point is to disallow a digit after the digit matching pattern, add `\d|` alternative into the negative lookahead. However, `\b[0-9]{1,4}[A-Za-z]\b` looks enough here. – Wiktor Stribiżew Aug 26 '22 at 11:49

0 Answers0