Extract 6 digit number from string python

An example string is "CPLR_DUK10_772989_2". I want to pick out "772989" specifically. I would imagine using re.findall is a good way to go about it, however, I don't have a very good grasp on regular expression so I find myself stumped on this one.

Here is a sample of code that I thought would work, until I looked at the full list of strings, and saw that it definitely doesn't. I suppose I'm looking for some more robustness!

for ad in Ads:
    num = ''.join(re.findall(numbers,ad)[1:7])
    ID.append(num)
ID = pd.Series(ID)

Other sample strings: "Teb1_110765", "PAN1_111572_5".

Extract 6 digit number from string python

TigerhawkT3

47.5k6 gold badges57 silver badges89 bronze badges

asked Apr 16, 2015 at 17:42

5

The regex you are looking for is

p = re.findall(r'_(\d{6})', ad)

This will match a six-digit number preceded by an underscore, and give you a list of all matches (should there be more than one)

Demo:

>>> import re
>>> stringy =  'CPLR_DUK10_772989_2'
>>> re.findall(r'_(\d{6})', stringy)
['772989']

answered Apr 16, 2015 at 17:52

Extract 6 digit number from string python

miradulomiradulo

27.4k6 gold badges75 silver badges92 bronze badges

5

This should append all sets of 6 numbers that follow an underscore

for ad in Ads:
    blocks = re.split('_', ad)
    for block in blocks[1:]:
        if len(block) == 6 and block.isdigit(): 
            ID.append(block)
ID = pd.Series(ID)

answered Apr 16, 2015 at 17:47

Extract 6 digit number from string python

Beth CraneBeth Crane

6253 silver badges8 bronze badges

2

You can use a list comprehension:

>>> s="CPLR_DUK10_772989_2"
>>> [x for x in s.split('_') if len(x)==6 and x.isdigit()]
['772989']

If your strings are really long and you are only looking for one number, you could use intertools like so:

>>> from itertools import dropwhile
>>> next(dropwhile(lambda x: not(len(x)==6 and x.isdigit()), s.split('_')))
'772989'

answered Apr 16, 2015 at 18:31

Extract 6 digit number from string python

dawgdawg

93.1k23 gold badges122 silver badges200 bronze badges

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    While programming, sometimes, we just require a certain type of data and need to discard other. This type of problem is quite common in Data Science domain, and since Data Science uses Python worldwide, its important to know how to extract specific elements. This article discusses certain ways in which only digit can be extracted. Let’s discuss the same.

    Method #1 : Using join() + isdigit() + filter()

    This task can be performed using the combination of above functions. The filter function filters the digits detected by the isdigit function and join function performs the task of reconstruction of join function.  

    Python3

    test_string = 'g1eeks4geeks5'

    print("The original string : " + test_string)

    res = ''.join(filter(lambda i: i.isdigit(), test_string))

    print("The digits string is : " + str(res))

    Output : 

    The original string : g1eeks4geeks5
    The digits string is : 145

     Method #2 : Using re 

    The regular expressions can also be used to perform this particular task. We can define the digit type requirement, using “\D”, and only digits are extracted from the string. 

    Python3

    import re

    test_string = 'g1eeks4geeks5'

    print("The original string : " + test_string)

    res = re.sub("\D", "", test_string)

    print("The digits string is : " + str(res))

    Output : 

    The original string : g1eeks4geeks5
    The digits string is : 145

    Method 3: Using loops:

    This task is done by using for loop.

    Python3

    s="g1eeks4geeks5"

    for i in s:

      if(i.isdigit()):

        print(i,end="")

    Output:
    The original string : g1eeks4geeks5
    The digits string is : 145

    Method 4: Using recursion:

    Python3

    def ExtractDigits(s,i=0):

        if i==len(s):

            return

        if s[i].isdigit():

            print(s[i], end='')

        i+=1

        ExtractDigits(s,i)

    s="g1eeks4geeks5"

    ExtractDigits(s)

    Method 5: Using ord() method 

    Python3

    s="g1eeks4geeks5"

    for i in s:

        if ord(i) in range(48,58):

            print(i,end="")


    How do you extract digits from a string in Python?

    This problem can be solved by using split function to convert string to list and then the list comprehension which can help us iterating through the list and isdigit function helps to get the digit out of a string.

    How do I extract digits from a string?

    The following example shows how you can use the replaceAll() method to extract all digits from a string in Java: // string contains numbers String str = "The price of the book is $49"; // extract digits only from strings String numberOnly = str. replaceAll("[^0-9]", ""); // print the digitts System. out.

    How do you find the number of numerical digits in a string in Python?

    Approach to Solve this Problem Take an input string. While iterating over the whole string, if we find a digit, then increment the count of digits; otherwise, if we find a letter, then increment the count of letters. Return the count of letters and digits as the output.