简体   繁体   中英

Remove everything that comes after a word that is after a specific word in Python

I have few strings like:

This is a beautiful string 1 - written by a dog
This is a beautiful string 2 written by a fly
This is a beautiful string 3 ewh

With python, I want to remove everything that comes after string X. After filtration I want the string to be:

This is a beautiful string 1
This is a beautiful string 2
This is a beautiful string 3

There can be anything after "string", it's not necessary that it'll be a number.

Is this possible?

I looked for ways of doing this and found.rsplit() but it didn't work the way I expected.

I guess simple regexp will do the job:

import re

p = re.compile("string \d+")
for line in lines:
    if resp := p.search(line):
        print(line[0:resp.end()])
    else:
        print(line)

Code you can try:

import re

s1 = "This is a beautiful string 1 - written by a dog"

my_regex = re.compile(r"(?P<final>This is a beautiful string)")

output_s1 = re.search(my_regex, s1)['final']
print(output_s1)

Output

"This is a beautiful string"

Assuming this is your string

 x = """This is a beautiful string 1 - written by a dog
    This is a beautiful string 2 written by a fly
    This is a beautiful string 3 ewh""" 

For single line:

print(x.replace((x[x.index("g")+3:]), ""))

output:

This is a beautiful string 1

For everyline:

print([y.replace((y[y.index("g")+3:]), "") for y in x.splitlines()])

output:

['This is a beautiful string 1', 'This is a beautiful string 2', 'This is a beautiful string 3']
x=This is a beautiful string 1 - written by a dog
y=This is a beautiful string 2 written by a fly
z=This is a beautiful string 3 ewh

list=[x,y,z]

string=[]
new_list=[]
for i in range (1,len(list)+1):
    sep='string {}'.format(i)
    string=list[i-1].split(sep,1)[0]+sep
    new_list.append(string)

new_list

Check results

Use re.match or re.findall :

import re

string1 = 'This is a beautiful string 1 - written by a dog'
string2 = 'This is a beautiful string 2 written by a fly'
string3 = 'This is a beautiful string 3 ewh'
string4 = 'This is a beautiful string whatever by me'

res1 = re.match('(.*string \w+)', string1)[0]
res2 = re.findall('(.*string \w+)', string2)[0]
res3 = re.match('(.*string \w+)', string3)[0]
res4 = re.findall('(.*string \w+)', string4)[0]

print(res1), print(res2), print(res3), print(res4)

# This is a beautiful string 1
# This is a beautiful string 2
# This is a beautiful string 3
# This is a beautiful string whatever

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM