I have a string like this:
s = "Abc 3456789 cbd 0045678 def 12345663333"
print(re.findall(r"(?<!\d)\d{7}(?!\d)", s))
Ouput is : 3456789 and 0045678
but I only want to get 3456789. How can I do that?
I have a string like this:
s = "Abc 3456789 cbd 0045678 def 12345663333"
print(re.findall(r"(?<!\d)\d{7}(?!\d)", s))
Ouput is : 3456789 and 0045678
but I only want to get 3456789. How can I do that?
As per title of finding 7 digit numbers that don't start with 0
you may use:
(?<!\d)[1-9]\d{6}(?!\d)
Note [1-9]
at start of match before matching next 6 digits to make it total 7 digits.
To make it match any number that doesn't start with 0
use:
(?<!\d)[1-9]\d*(?!\d)
This will do it: ^\D+(\d+)\s
At the beginning of the string ^
, there are any non digit characters \D+
, followed by any number of digits \d+
, which are captured (\d+)
, and need to be followed by a whitespace \s
.
If you are looking for number not beginning with 0, then use [1-9]
for the first digit and \d
for the remaining digits.
For example, to find the ones of length 7 (as per the title), this would give you:
re.findall(r'(?<!\d)[1-9]\d{6}(?!\d)', s)
in other words, a non-zero digit followed by 6 digits, the whole thing neither preceded nor followed by a digit (per the negative lookahead and negative lookbehind assertions),
which for your current example string would produce:
['3456789']
If you want ones that are not length 7, you could use:
re.findall(r'(?<!\d)[1-9](?:\d{,5}|\d{7,})(?!\d)', s)
in other words, a non-zero digit followed by either <= 5 or >= 7 digits (i.e. any number other than 6), the whole thing neither preceded nor followed by a digit,
which would give:
['12345663333']
Note in the second case the use of ?:
to ensure that the bracketed group is a non-capturing one -- this ensures that re.findall
will return everything that is matched, rather than the contents of the parentheses.