Reputation: 21
Say I have a string "ldhjshjds HdAjhdshj4 Hdsshj4 kdskjdshjdsjds"
I only want to search for substrings (alphanumeric only) starting with "H", but only if the string is between 10-20 characters.
"HdAjhdshj4" would be a match. "Hdsshj4" would not.
Would such a regex be costly on CPU cycles?
Upvotes: 1
Views: 1833
Reputation: 67968
You can do this using lookarounds
.
re.findall(r'(?:^|(?<=\s))H[A-Za-z0-9]{9,19}(?=\s|$)', s)
Upvotes: 0
Reputation: 174696
Use negative lookarounds.
re.findall(r'(?<!\S)H[A-Za-z0-9]{9,19}(?!\S)', s)
Upvotes: 0