-2

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?

kanaga
  • 17
  • 5
  • 1
    Both 45678 and 00456 are not 7 digit numbers – anubhava Oct 09 '20 at 06:36
  • It seems from your "I only want to get 45678" that you maybe you want numbers that are *not* 7 digits long (despite the title). The question is a bit confusing because of this. – alani Oct 09 '20 at 06:54
  • It looks like the example string has totally changed since I started answering. And the question is still wrong: the output of the command you use is *not* what you claim it to be. And when you say that you want to get 45678, this makes even less sense now. – alani Oct 09 '20 at 07:02

3 Answers3

2

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.

RegEx Demo

To make it match any number that doesn't start with 0 use:

(?<!\d)[1-9]\d*(?!\d)
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

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.

See: https://regex101.com/r/ZuGJ7l/1

Christian Baumann
  • 3,188
  • 3
  • 20
  • 37
0

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.

alani
  • 12,573
  • 2
  • 13
  • 23
  • 1
    I noticed after posting this that Anu Bhava's solution already posted has the same regex for getting numbers that **do** have 7 digits. However, there is significant confusion about whether the question is looking for numbers that **do** or **don't** have 7 digits, because the example in the question (namely: "I only want to get 45678") contradicts the title. As this answer also addresses that case (*not* 7 digits), I am going to leave it here. – alani Oct 09 '20 at 06:49
  • Answer now updated to reflect changed examples in the question. – alani Oct 09 '20 at 07:06
  • yes. I'm sorry for this case I was uncareful. I have updated it and your answer is totally right too but I only can choose one answer. Thank you so much for your help. – kanaga Oct 10 '20 at 08:15