0

I'm pretty new to Python and I just started learning regex. I'm trying to define a function that takes as input a string containing zero or more sequences of digits and returns a list of all sequences of digits in the string and sorts them. If there isn't it will print that there isn't any.

Here's my code:

def DigitSequence(string):
    pattern=re.compile(r'[0-9]+')
    k=re.findall(pattern,string)
    if len(k)!=0:
        return(sorted(k))
    else:
        return('None')

print( DigitSequence("My number is 98765 and my friend\'s number is 12345.") )
#This should return me ['12345','98765']. And it does.

But is there any better way to do it. Are there any cases I would miss if I do this. I thought of using pattern=re.compile(r'\d*') but I felt it was equivalent to what I did. Correct me if I am wrong.

6
  • 1
    \d is exactly the same as [0-9], however the quantifiers you used in those patterns are different. * matches the preceding entity zero or more times, while + matches one or more times. So yeah, in many cases those two patterns will give you the same result, but there's an important distinction. Commented Mar 10, 2020 at 23:33
  • Yes. I got the same result. Commented Mar 10, 2020 at 23:34
  • What about the cases...would I be missing any. Should I put + or * so that the result won't be changed in case there's an alphanumeric term in the string. Commented Mar 10, 2020 at 23:36
  • Ohh yeah..you mean the spaces. I did get those so i wrote another line to clean them but i felt thats not the best way to do it. Commented Mar 10, 2020 at 23:38
  • @HeapOverflow So you mean this won't work for single digits? Then how can I capture both? Is there such a regex term or can i write some condition: pattern=re.compile(r'\d+|\d') . something like this? Commented Mar 10, 2020 at 23:45

1 Answer 1

1

re.findall() is on the right track. Some additional points to note:

  1. An uncompiled regex string can be passed directly into re.findall().
  2. If full-width digits are wanted, use r"\d+". Otherwise use "[0-9]+".
  3. Sorting order: 99 before 111? Or '1'11 before '9'9?
  4. Separate printing logic and parsing logic.

Code:

import re

def digital_sequence(s, sort_literal=False, fullwidth_digits=False):
    """Get sorted list of numbers.

    Args:
        s (str): arbitrary string
        sort_literal (bool): literal sort
        fullwidth_digits (bool): accept full-width digits

    Returns:
        list
    """
    r = r"\d+" if fullwidth_digits else r"[0-9]+"
    k = None if sort_literal else lambda x: int(x)
    return sorted(re.findall(r, s), key=k)

def print_ls(ls):
    """Customized list printing: list contents or "None".

    Args:
        ls (list): input list

    Returns:
        None
    """
    print(ls) if len(ls) > 0 else print("None")

s1 = "99 and 112 and 12"
s2 = "abc"
print_ls(digital_sequence(s1))  # ['99', '112']
print_ls(digital_sequence(s2))  # None
print_ls(digital_sequence(s1, sort_literal=True))  # ['112', '99']
print_ls(digital_sequence(s1, fullwidth_digits=True))  # ['12', '99', '112']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.