Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find numbers using Regex
#16
When you know what you want to replace it is easy to remove the digits. This uses a comprehension strip the numbers.:
from io import StringIO
import re


test_text = StringIO(
"""25 - not this number1
the cow just over the moon and the sun is in 1the sky
26 - not this number
5one day is soon and soon is near take 5529care, 30over and out
59 - not this number
The covers at near the back of the 59closet, and when found have them place on the each of the beds. However you see the pillow cases use the ones on the 9second shelve."""
)

pattern = re.compile(r"[0-9]+[a-zA-Z]+|[a-zA-Z]+[0-9]+")
for line in test_text:
    matches = re.findall(pattern, line)
    if matches:
        for match in matches:
            line = line.replace(match, "".join([c for c in match if c not in '0123456789']))
    print(line.rstrip())
Output:
25 - not this number the cow just over the moon and the sun is in the sky 26 - not this number one day is soon and soon is near take care, over and out 59 - not this number The covers at near the back of the closet, and when found have them place on the each of the beds. However you see the pillow cases use the ones on the second shelve.
And this uses another regex.
        for match in matches:
            line = line.replace(match, re.findall(stripper, match)[0])
But it is better to use re.sub(). Write a function that returns a digit-less version of the matching string. This function is the repl argument to the re.sub(patter, repl, string) call.
from io import StringIO
import re

test_text = StringIO(
"""25 - not this number1
the cow just over the moon and the sun is in 1the sky
26 - not this number
5one day is soon and soon is near take 5529care, 30over and out
59 - not this number
The covers at near the back of the 59closet, and when found have them place on the each of the beds. However you see the pillow cases use the ones on the 9second shelve."""
)

stripper = re.compile(r"[a-zA-Z]+")
finder = re.compile(r"[0-9]+[a-zA-Z]+|[a-zA-Z]+[0-9]+")

def strip_digits(match):
    """This is the repl function used by re.sub()"""
    return re.findall(stripper, match.group())[0]

for line in test_text:
    print(re.sub(finder, strip_digits, line).rstrip())
rob101 likes this post
Reply


Messages In This Thread
Find numbers using Regex - by giddyhead - Jul-24-2022, 04:41 AM
RE: Find numbers using Regex - by rob101 - Jul-24-2022, 06:26 AM
RE: Find numbers using Regex - by giddyhead - Jul-24-2022, 12:11 PM
RE: Find numbers using Regex - by rob101 - Jul-24-2022, 02:52 PM
RE: Find numbers using Regex - by giddyhead - Jul-24-2022, 06:22 PM
RE: Find numbers using Regex - by Pedroski55 - Jul-24-2022, 07:29 AM
RE: Find numbers using Regex - by rob101 - Jul-24-2022, 06:36 PM
RE: Find numbers using Regex - by giddyhead - Jul-24-2022, 08:32 PM
RE: Find numbers using Regex - by rob101 - Jul-24-2022, 11:03 PM
RE: Find numbers using Regex - by giddyhead - Jul-25-2022, 05:27 AM
RE: Find numbers using Regex - by Pedroski55 - Jul-25-2022, 12:26 AM
RE: Find numbers using Regex - by giddyhead - Jul-25-2022, 05:22 AM
RE: Find numbers using Regex - by deanhystad - Jul-25-2022, 04:26 AM
RE: Find numbers using Regex - by giddyhead - Jul-25-2022, 05:15 AM
RE: Find numbers using Regex - by rob101 - Jul-25-2022, 06:07 AM
RE: Find numbers using Regex - by deanhystad - Jul-25-2022, 06:28 AM
RE: Find numbers using Regex - by snippsat - Jul-25-2022, 07:32 PM
RE: Find numbers using Regex - by giddyhead - Jul-26-2022, 12:54 AM
RE: Find numbers using Regex - by giddyhead - Jul-28-2022, 12:29 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Regex to find triple characters bfallert 14 487 May-16-2024, 04:02 PM
Last Post: xMaxrayx
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,444 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Find and Replace numbers in String giddyhead 2 1,296 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  find 2 largest equal numbers Frankduc 13 3,709 Jan-11-2022, 07:10 PM
Last Post: Frankduc
  Find and replace in files with regex and Python Melcu54 0 1,878 Jun-03-2021, 09:33 AM
Last Post: Melcu54
  Find and replace to capitalize with Regex hermobot 2 2,576 Mar-21-2020, 12:30 PM
Last Post: hermobot
  Python regex to get only numbers tantony 6 4,209 Oct-09-2019, 11:53 PM
Last Post: newbieAuggie2019
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,822 May-09-2019, 12:19 PM
Last Post: Pleiades
  How to find the sum of even numbers from entered N numbers? Rajath 2 12,446 Sep-13-2017, 07:19 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020