Say I have a number: 6278041121932517
If I want to get 11219325
I have a regex of:
re.search(r"((1|2)\d{7})", "6278041121932517")
but the thing is, it might begin with a 1 or a 2. So I want to add a minimum buffer beforehand say, 4 digits. I thought about look-behind, but it doesn't support n lengths.
It seems you may use
^\d{4,}?([12]\d{7})
See the regex demo
Details:
^
- start of string\d{4,}?
- 4 or more digits, but as few as possible([12]\d{7})
- Group 1 (your value):
[12]
- 1
or 2
\d{7}
- 7 digitsSee Python demo:
import re
m = re.search(r"^\d{4,}?([12]\d{7})", "6278041121932517")
if m:
print(m.group(1))
# => 11219325
If all you want is a "buffer," you don't even need to change the regex for that. Just slice off a few characters.
>>> import re
>>> re.search(r"((1|2)\d{7})", "6278041121932517").group()
'27804112'
>>> re.search(r"((1|2)\d{7})", "6278041121932517"[4:]).group()
'11219325'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With