<개인공부> - IT/[Python]
Python regular expression match and search methods
Aggies '19
2020. 3. 24. 10:23
반응형
Long story short, match and search methods can be used whether to search the beginning of the string or the entire string.
Let's dig into an example.
import re
data = ["ESA1-XXX-XXX - Po1", "ESA1-XXX-XXX - PO1"]
for elem in data:
m = re.search('(Po|PO)\d', elem).group()
What I want to do is to find and replace "- Po1" or "- PO1" to "(Po1)" or "(PO1)".
In order to find a certain pattern, I use re module.
Po1 and PO1 patterns are at the end of the string so match method always returns none.
Instead of the match method, the search method works well.
반응형