When dealing with text data that contains both text and numbers, it can be necessary to extract the numerical values from the string. Two common methods for performing this task in Python are regular expressions and the isdigit() method.
Regular expressions (regex) offer a powerful way to match patterns within a string. To extract numbers, you can use the d pattern, which matches one or more digits. Consider the example:
import re line = "hello 12 hi 89" numbers = re.findall(r'\d+', line) print(numbers) # Output: [12, 89]
This regex matches the sequences of digits "12" and "89" and returns them as a list. To match only numbers that are delimited by word boundaries (such as spaces, periods, or commas), you can use the b pattern:
numbers = re.findall(r'\b\d+\b', line) print(numbers) # Output: [12, 89]
The isdigit() method provides an alternative way to extract numbers from a string. This method returns True for characters that are digits and False otherwise. To use it, you can iterate through the string and check each character:
line = "hello 12 hi 89" numbers = [] for char in line: if char.isdigit(): numbers.append(int(char)) print(numbers) # Output: [12, 89]
In this example, the method iterates through each character in the string, checks if it is a digit using isdigit(), and appends it to the numbers list if it is.
Generally, using regular expressions is more efficient for extracting numbers from a string than using the isdigit() method. Regular expressions can match complex patterns quickly, while isdigit() requires iterating through the entire string.
If you need to extract negative integers as well, the regex approach provides an easier solution. Simply use r'-d ' to match negative digits. However, using isdigit() would require more complex logic to handle negative signs.
The above is the detailed content of Python String Number Extraction: Regex vs. isdigit() - Which Method is Best?. For more information, please follow other related articles on the PHP Chinese website!