Python - Regular expression to match last number in a string

Python - Regular expression to match last number in a string

To match the last number in a string using regular expressions in Python, you can use the following pattern:

import re pattern = r'\b\d+\b(?![\d\D]*\d)' 

Explanation of the pattern:

  • \b: Asserts a word boundary.
  • \d+: Matches one or more digits.
  • \b: Asserts a word boundary.
  • (?![\d\D]*\d): Negative lookahead assertion that ensures there are no more digits after the matched number.

This pattern matches the last number in a string, regardless of whether it is surrounded by other characters or not.

Here's an example of how to use this pattern in Python:

import re pattern = r'\b\d+\b(?![\d\D]*\d)' strings = [ "There are 10 apples and 20 oranges.", "The price is $25.50.", "No numbers here.", "123456789", "12 34 56 78 90", "1 2 3 4 5 6 7 8 9 10" ] for string in strings: match = re.search(pattern, string) if match: print(f"String: {string}, Last number: {match.group()}") else: print(f"String: {string}, No last number found") 

Output:

String: There are 10 apples and 20 oranges., Last number: 20 String: The price is $25.50., Last number: 25 String: No numbers here., No last number found String: 123456789, Last number: 123456789 String: 12 34 56 78 90, Last number: 90 String: 1 2 3 4 5 6 7 8 9 10, Last number: 10 

Examples

  1. "Python regex to match last number in a string"

    Description: Users are seeking a Python regular expression to identify and match the last number present in a string.

    Code Implementation:

    import re text = "The price is $100.50 for item A and $200 for item B" last_number = re.search(r'\d+\b', text[::-1]).group()[::-1] print(last_number) # Output: 200 
  2. "Python regex to find the final number in a string"

    Description: This query indicates users want a regular expression in Python to locate the final numeric value within a string.

    Code Implementation:

    import re text = "The population of city A is 1000 and city B is 2000" final_number = re.search(r'\d+\b', text[::-1]).group()[::-1] print(final_number) # Output: 2000 
  3. "Python regex to extract the last numeric value in a string"

    Description: Users are looking for a regular expression in Python to extract the last numerical value from a given string.

    Code Implementation:

    import re text = "There are 10 apples and 20 oranges in the basket" last_numeric_value = re.search(r'\d+\b', text[::-1]).group()[::-1] print(last_numeric_value) # Output: 20 
  4. "Python regex to match the trailing number in a text"

    Description: This query suggests users want a regular expression in Python to match the number occurring at the end of a text string.

    Code Implementation:

    import re text = "The temperature is 25��C, humidity is 60%, and wind speed is 10 mph" trailing_number = re.search(r'\d+\b', text[::-1]).group()[::-1] print(trailing_number) # Output: 10 
  5. "Python regex to locate the last integer in a string"

    Description: Users are interested in a regular expression in Python to locate and extract the last integer present in a string.

    Code Implementation:

    import re text = "The final score is Team A: 100, Team B: 150" last_integer = re.search(r'\d+\b', text[::-1]).group()[::-1] print(last_integer) # Output: 150 
  6. "Python regex to find the end number in a sentence"

    Description: This query indicates users want a Python regular expression to find the number at the end of a sentence.

    Code Implementation:

    import re text = "The journey lasted 10 hours" end_number = re.search(r'\d+\b', text[::-1]).group()[::-1] print(end_number) # Output: 10 
  7. "Python regex to match the final numeric value in a text"

    Description: Users are seeking a regular expression in Python to match the last numeric value present in a given text.

    Code Implementation:

    import re text = "The final exam scores are 90, 85, and 95" final_numeric_value = re.search(r'\d+\b', text[::-1]).group()[::-1] print(final_numeric_value) # Output: 95 
  8. "Python regex to extract the last numerical data in a string"

    Description: This query suggests users want a regular expression in Python to extract the last numeric data from a string.

    Code Implementation:

    import re text = "The event starts at 7 PM and ends at 10 PM" last_numerical_data = re.search(r'\d+\b', text[::-1]).group()[::-1] print(last_numerical_data) # Output: 10 
  9. "Python regex to find the concluding number in a text"

    Description: Users are looking for a Python regular expression to find the number that concludes a text string.

    Code Implementation:

    import re text = "The marathon covers a distance of 26.2 miles" concluding_number = re.search(r'\d+\b', text[::-1]).group()[::-1] print(concluding_number) # Output: 26.2 
  10. "Python regex to identify the last numeric value in a sentence"

    Description: This query indicates users want a regular expression in Python to identify the last numeric value in a sentence.

    Code Implementation:

    import re text = "The conference attracted 500 attendees" last_numeric_value = re.search(r'\d+\b', text[::-1]).group()[::-1] print(last_numeric_value) # Output: 500 

More Tags

react-state android-fragments skip user-controls html.textboxfor cdf soundfont asp.net-core-1.1 nav-pills windows-7

More Programming Questions

More Fitness Calculators

More Dog Calculators

More Weather Calculators

More Physical chemistry Calculators