Python Forum
Find and Replace numbers in String
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find and Replace numbers in String
#1
Hello everyone

I am looking for a way to take out numbers attached to concat string in paragraphs.

Before:
For example Paragraph:

1
It was 1their..some text, some text

2
This is one thing 2God....some text, some text

3
For 3He is.....some text, some text

150
5The people of....some text, some text

151
etc some text, some text

After:

For example Paragraph:

1
It was their..some text, some text

2
This is one thing God....some text, some text

3
For He is.....some text, some text

150
The people of....some text, some text

151
etc


The following regex is what I currently have :
re.sub('(?<=[ \t])?\d+\S\S+\S*','',txt)
and replaces the whole string instead of leaving only the words. How can I fix this task? Thanks
Reply
#2
Might could do something like

string = 'It was 1there...some text, some text'
for word in string:
    if word.isnumeric():
        string = string.replace(word, '')
print(string)
Output:
It was there...some text, some text
Of coarse it would be better suited to make it a function. This will remove all numbers.
rob101 likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(Jul-16-2022, 11:08 PM)menator01 Wrote: Might could do something like

string = 'It was 1there...some text, some text'
for word in string:
    if word.isnumeric():
        string = string.replace(word, '')
print(string)
Output:
It was there...some text, some text
Of coarse it would be better suited to make it a function

Thanks for the help and reply. Please mark this as complete.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to replace a string with a file (HTML file) tester_V 1 764 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Pulling Specifics Words/Numbers from String bigpapa 2 760 May-01-2023, 07:22 PM
Last Post: bigpapa
  How do I check if the first X characters of a string are numbers? FirstBornAlbratross 6 1,530 Apr-12-2023, 10:39 AM
Last Post: jefsummers
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,211 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Replace string in a nested Dictianory. SpongeB0B 2 1,202 Mar-24-2023, 05:09 PM
Last Post: SpongeB0B
  Working with Excel and Word, Several Questions Regarding Find and Replace Brandon_Pickert 4 1,551 Feb-11-2023, 03:59 PM
Last Post: Brandon_Pickert
  Replace with upper(string) WJSwan 7 1,586 Feb-10-2023, 10:28 AM
Last Post: WJSwan
  Find numbers using Regex giddyhead 18 3,150 Jul-28-2022, 12:29 AM
Last Post: giddyhead
  find 2 largest equal numbers Frankduc 13 3,545 Jan-11-2022, 07:10 PM
Last Post: Frankduc
  Replace String in multiple text-files [SOLVED] AlphaInc 5 8,132 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

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