Python Forum
Find and Replace numbers in String - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Find and Replace numbers in String (/thread-37745.html)



Find and Replace numbers in String - giddyhead - Jul-16-2022

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


RE: Find and Replace numbers in String - menator01 - Jul-16-2022

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.


RE: Find and Replace numbers in String - giddyhead - Jul-17-2022

(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.