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
.
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']
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
.
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']