Regex to exclude strings of length more than 10

Question:

Code:

re.findall(r'A[0-9]+', text, re.I)

How to modify the regex to restrict it from returning anything where number of matched characters is greater than 10?
If I use {,10}, it will simply truncate instead of ignoring.

Example:

text = A555 A12345678901

It should return only A555.

Asked By: aste123

||

Answers:

Another solution (with word boundaries b):

text = "A555 A12345678901 A123456789"

print(re.findall(r"b[A-Z][0-9]{1,10}b", text))

Prints:

['A555', 'A123456789']
Answered By: Andrej Kesely
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.