I am learning and trying regex on a string.
which is "DBZ:00000*{6000}/ONE/REFFERRARO REF:FINE DOGS*"
I am trying to find all the REF
in this string. So I used this:
import re
doom = 'REF'
boom = "DBZ:00000*{6000}/ONE/REFFERRARO REF:FINE DOGS*"
# print(i)
# print('Found "%s" in "%s" ->' % (i, boom), end='')
print(re.findall(r"\b" + doom + "*", boom))
if re.search(doom, boom):
print("found")
Output:
['REFF','REF']
I am not here getting exact REF and also what I want to do is check if there is any character next to "REF"
Like: "REFFERRARO" -> Here next to "REF" is "F"
"REF:FINE" -> Here next to "REF" is ":"
So If I find next to "REF"
anything except ":"
I want to add ":"
after "REF"
.
Example:
String: "DBZ:00000*{6000}/ONE/REFFERRARO REF:FINE DOGS*"
Output: "DBZ:00000*{6000}/ONE/REF:FERRARO REF:FINE DOGS*"
UPDATE:
As said I used .sub and got this:
print(re.compile('REF').sub("REF:", boom))
Output:
"DBZ:00000*{6000}/ONE/REF:FERRARO REF::FINE DOGS*"
New UPDATE:
Tried this and it worked (But I don't think this is valid because if there are n number of "REFFERRARO" then):
print(re.compile('REF').sub("REF:", boom,count=1))