find number sequence in a string using regex in python in python

You can use regular expressions (regex) in Python to find number sequences in a string using the re module. Here's an example code snippet:

main.py
import re

# Sample string
string = "Hello 1234 World 5678"

# Regex pattern to match number sequences
pattern = r"\d+"

# Find all matches in the string
matches = re.findall(pattern, string)

# Print the matches
print(matches)  # Output: ["1234", "5678"]
256 chars
14 lines

In the above code, we import the re module and define a string that contains number sequences. We then define a regex pattern that matches one or more digits (\d+). We use the re.findall() function to find all non-overlapping matches of the pattern in the string, and store the matches in a list called matches. Finally, we print the matches.

gistlibby LogSnag