简体   繁体   中英

How to remove text after and before specific words in a string using python regex

I have a string "copy table a (no = 1, name = xyz, city = c0nl ) from 'a.dat';". In this I want to remove the words within 'copy' and 'from', but need file-name as: my desirable output is "copy a from a.dat;"

Any help would be great. I want to use regular expression for that.

You can use the regex module re and the function sub (replace/substitute) in conjunction with lookahead (?=from) and lookbehind (?<=copy ) - also referred to as lookaround , in order to remove only the requested part (.*) that comes in-between:

import re
print re.sub(r'(?<=copy )(.*)(?=from)', '', "copy table values from 'a.dat';")

OUTPUT

copy from 'a.dat';

You can do:

import re
mystr = "copy table values from 'a.dat';"
print(re.sub('copy.*from', 'copy from', mystr))

And you don't worry about spaces, greedyness and all that.

(?<=\bcopy\b)[\s\S]*?(?=\s*\bfrom\b)

Use \\b and lookarounds .See demo.

https://regex101.com/r/sS2dM8/11

import re
p = re.compile(r'(?<=\bcopy\b)[\s\S]*?(?=\s*\bfrom\b)', re.MULTILINE)
test_str = "copy table values from 'a.dat';"
subst = ""

result = re.sub(p, subst, test_str)

Output: copy from 'a.dat';

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