hope someone could help me. I am new to python and just learning. I would like to know how to delete unwanted characters from a string.
For example,
I have some strings in a text file such as 'dogs op care 6A domain, cats op pv=2 domain 3, pig op care2 domain 3'
I don't need anything after that starts with op. i.e., what I would like to get is just 'dogs, cats, pig'
I see 'op' as the pattern in all these strings and therefore tried the below code
import re
f = open('animalsop.txt','r')
s = f.read()
p = re.compile('op')
match = p.search(s)
print (s[:match.start()])
The output I get is just 'dog'
why do I not get the cat and pig as well since they contain 'op' too.
Any help would be greatly appreciated because I would the code to analyse a huge similar data I have got.
The above code was derived from String splitting in Python using regex
credits to Varuna and kragniz
dog opportunities, some answers here may break. dr jimbob's looks for spaces on either side. If you do use regex, you should use\bop\b, which ensures that what precedes/followedopis a non-word character (nota-zA-Z0-9_), or ` op ` which does pretty much what dr jimbob's answer does but in regex