Last Updated on 2022-08-29 by Clay
If we need to match a special keyword with Python, and add HTML tags to the left and right sides, what should we do?
For example, we have to mark the <span style="color: red;">
and </span>
on both sides of the "NICE"
string to make it show different colors in HTML text.
Very intuitive, everyone should think of the regular expression! You can call the native re
module directly in Python.
Outside of the question, although the python string has a .replace()
method, it does not support the regular expression; if you want to keep the original keyword, you need to spend more work time.
So, how should we do?
Use \b
to match the boundary of keyword
In regular expression, we can use \b
to match the boundary of the keyword you want, and then we can insert any tag on the left and right sides.
Let's take an example.
# coding: utf-8
import re
def main() -> None:
text = "Today is a nice day! VERY NICE!!!"
pattern = re.compile(r"\b(nice)\b", re.IGNORECASE)
new_text = pattern.sub("<span style='color: red'>\\1</span>", text)
print(new_text)
if __name__ == "__main__":
main()
Output:
Today is a <span style='color: red'>nice</span> day! VERY <span style='color: red'>NICE</span>!!!
Yes! we did it! We keep the keyword NICE
and insert the HTML tag.
On the browser, it looks like:
References
- Python Regex Replace Pattern in a string using re.sub()
- How do I write a regex to replace a word but keep its case in ...