This question already has answers here:
How to use a variable inside a regular expression?
(12 answers)
Closed 3 years ago.
I have a function with which I'm using regular expressions to replace words in sentences.
The function that I have looks as follows:
def replaceName(text, name):
newText = re.sub(r"\bname\b", "visitor", text)
return str(newText)
To illustrate:
text = "The sun is shining"
name = "sun"
print(re.sub((r"\bsun\b", "visitor", "The sun is shining"))
>>> "The visitor is shining"
However:
replaceName(text,name)
>>> "The sun is shining"
I think this doesn't work because I'm using the name of a string (name in this case) rather than the string itself. Who knows what I can do so this function works?
I have considered:
Using variable for re.sub,
however although the name is similar, its a different question.
Python use variable in re.sub, however this is just about date and time.
You can use string formatting here:
def replaceName(text, name):
newText = re.sub(r"\b{}\b".format(name), "visitor", text)
return str(newText)
Otherwise in your case re.sub is just looking for the exact match "\bname\b".
text = "The sun is shining"
name = "sun"
replaceName(text,name)
# 'The visitor is shining'
Or for python versions of 3.6< you can use f-strings as #wiktor has pointed out in the comments:
def replaceName(text, name):
newText = re.sub(rf"\b{name}\b", "visitor", text)
return str(newText)
Related
This question already has answers here:
How do I check for an exact word or phrase in a string in Python
(8 answers)
Closed 2 years ago.
message = "I'm new and this is new my account."
The program will try to detect 'hi' in this string even there is no 'hi' in here, it will found the keyword in "and this is m..." part if try to use a code like this:
if "hi" in message.lower():
print("He said hi!")
How can I block this out?
You can use regular expressions.
import re
message = "I'm new and this is new my account."
message_with_hi = "what's up, I'm saying hi"
pattern = r'\bhi\b' # \b is word boundary
r = re.findall(pattern, message)
r2 = re.findall(pattern, message_with_hi)
print(r) # prints []
print(r2) # prints ['hi']
This also covers cases like message = "I am saying hi!".
An elegant solution would be:
if ' hi ' in f' {message} ':
print("He said hi!")
Or using regex: https://stackoverflow.com/a/5320179/4585157
This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
python regular expression across multiple lines
(2 answers)
Closed 2 years ago.
I have a long string with some placeholders like %name% which should be substituted with a value given by a dict. According to this link, I was able to solve it. Some parts however should only be included in the returned string, if another parameter is True. For example with this formatting:
>>optional:This should only get printed, if 'optional' is True<<
I might get it to work, but I was not able to create a regex expression that works also with multiline
import re
# This works (https://stackoverflow.com/questions/26844742/advanced-string-replacements-in-python)
def replaceParameter(string, replacements):
return re.sub('%(\w+)%', lambda m: replacements[m.group(1)], string)
# This does not work
def replaceOptionalText(myString, replacements):
occurences = re.findall(">>(.*?):(.*)<<", myString, re.MULTILINE)
# ... #
myLongString = r"""My name is %name%.
I want to >>eat:eat some %food%.
(in two lines)<<
>>drink:drink something<<
"""
replacements = {
'name': 'John',
'eat': True,
'food': 'Apples',
'drink': False,
}
myLongString = replaceOptionalText(myLongString, replacements)
myLongString = replaceParameter(myLongString, replacements)
print(myLongString)
with the expected Output:
My name is John.
I want to eat some Apples.
(in two lines)
This question already has answers here:
How to match a whole word with a regular expression?
(4 answers)
Closed 3 years ago.
I have a function with which I want to anonymize texts by replacing the name of a person by 'visitor'.
To do so, I have written the following function:
def replaceName(text, name):
newText = text.replace(name, 'visitor')
return str(newText)
And I apply it using:
all_transcripts['msgText'] = all_transcripts.apply(lambda x: replaceName(x['msgText'], x['nameGuest']), axis=1)
However, this also replaces the name if it is a part of another word. Therefore, I want to only replace the instances where this word stands by itself. I have tried it as " "+name+" ", however, this does not work if the name is at the beginning or end of a sentence.
Furthermore, I have considered: Python regular expression match whole word. However, here they say how to find such words, but not how to replace it. I am having trouble to both find and replace it.
Who can help me with this?
import re
text = "Mark this isMark example Mark."
print (re.sub(r"\bMark\b", "visitor", text))
output:
visitor this isMark example visitor.
This question already has answers here:
Python replace string pattern with output of function
(4 answers)
Closed 5 years ago.
Say I have the following string:
mystr = "6374696f6e20????28??????2c??2c????29"
And I want to replace every sequence of "??" with its length\2. So for the example above, I want to get the following result:
mystr = "6374696f6e2022832c12c229"
Meaning:
???? replaced with 2
?????? replaced with 3
?? replaced with 1
???? replaced with 2
I tried the following but I'm not sure it's the good approach, and anyway -- it doesn't work:
regex = re.compile('(\?+)')
matches = regex.findall(mystr)
if matches:
for match in matches:
match_length = len(match)/2
if (match_length > 0):
mystr= regex .sub(match_length , mystr)
You can use a callback function in Python's re.sub. FYI lambda expressions are shorthand to create anonymous functions.
See code in use here
import re
mystr = "6374696f6e20????28??????2c??2c????29"
regex = re.compile(r"\?+")
print(re.sub(regex, lambda m: str(int(len(m.group())/2)), mystr))
There seems to be uncertainty about what should happen in the case of ???. The above code will result in 1 since it converts to int. Without int conversion the result would be 1.0. If you want to ??? to become 1? you can use the pattern (?:\?{2})+ instead.
This question already has answers here:
What is the difference between re.search and re.match?
(9 answers)
Closed 9 years ago.
I am very new to regex and learning by practice. I wrote the following regex for finding a number inside a string of characters, however, it returns nothing. Why is that?
string = "hello world & bello stack 12456";
findObj = re.match(r'[0-9]+',string,re.I);
if findObj:
print findObj.group();
else:
print "nothing matched"
Regards
re.match must match from the beginning of the string.
Use re.search instead.
re.match matches from the start of the string. Use re.search
>>> my_string = "hello world & bello stack 12456"
>>> find_obj = re.search(r'[0-9]+', my_string, re.I)
>>> print find_obj.group()
12456
P.S semicolons are not necessary.