how to do replacement of mulile string with multiple values in regex python
Question:
Given the following string:
"""
All I want is a proper cup of coffee
Made in a proper copper coffee pot
I may be off my dot
But I want a cup of coffee
From a proper coffee pot
Tin coffee pots and iron coffee pots
They’re no use to me
If I can’t have a proper cup of coffee
In a proper copper coffee pot
I’ll have a cup of tea.
"""
Using regular expressions, i need to write a function that highlight the words “coffee”,“pot” or “pots”
if they appear at the end of a line
so the patter i tried is
coffee$|pot$|pots$
( since $ is used for endwith)
if i do in regex101 it s highlighting all the required words
hoewever output in jupyter notebook is
'All I want is a proper cup of coffeenMade in a proper copper coffee potnI may be off my dotnBut I want a cup of coffeenFrom a proper coffee potnTin coffee pots and iron coffee potsnThey’re no use to menIf I can’t have a proper cup of coffeenIn a proper copper coffee potnI’ll have a cup of tea'
i tried with coffee$ coffee$n coffeen$ nothing works here. if i use .replace("n", " ")
it its taking as whole string. how to handle n in jupyter notebook
Answers:
I’m not sure what you mean by "highlight", but here’s an example of a RegEx that matches all instances "coffee", "pot", and "pots" at the end of a line:
import re
string = """
All I want is a proper cup of coffee
Made in a proper copper coffee pot
I may be off my dot
But I want a cup of coffee
From a proper coffee pot
Tin coffee pots and iron coffee pots
They’re no use to me
If I can’t have a proper cup of coffee
In a proper copper coffee pot
I’ll have a cup of tea.
"""
pattern = re.compile(r'(coffee|pots?)(?:n)')
print(re.findall(pattern, string))
See here for a breakdown of the RegEx pattern used.
Given the following string:
"""
All I want is a proper cup of coffee
Made in a proper copper coffee pot
I may be off my dot
But I want a cup of coffee
From a proper coffee pot
Tin coffee pots and iron coffee pots
They’re no use to me
If I can’t have a proper cup of coffee
In a proper copper coffee pot
I’ll have a cup of tea.
"""
Using regular expressions, i need to write a function that highlight the words “coffee”,“pot” or “pots”
if they appear at the end of a line
so the patter i tried is
coffee$|pot$|pots$
( since $ is used for endwith)
if i do in regex101 it s highlighting all the required words
hoewever output in jupyter notebook is
'All I want is a proper cup of coffeenMade in a proper copper coffee potnI may be off my dotnBut I want a cup of coffeenFrom a proper coffee potnTin coffee pots and iron coffee potsnThey’re no use to menIf I can’t have a proper cup of coffeenIn a proper copper coffee potnI’ll have a cup of tea'
i tried with coffee$ coffee$n coffeen$ nothing works here. if i use .replace("n", " ")
it its taking as whole string. how to handle n in jupyter notebook
I’m not sure what you mean by "highlight", but here’s an example of a RegEx that matches all instances "coffee", "pot", and "pots" at the end of a line:
import re
string = """
All I want is a proper cup of coffee
Made in a proper copper coffee pot
I may be off my dot
But I want a cup of coffee
From a proper coffee pot
Tin coffee pots and iron coffee pots
They’re no use to me
If I can’t have a proper cup of coffee
In a proper copper coffee pot
I’ll have a cup of tea.
"""
pattern = re.compile(r'(coffee|pots?)(?:n)')
print(re.findall(pattern, string))
See here for a breakdown of the RegEx pattern used.