반응형
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.
반응형
'<개인공부> - IT > [Python]' 카테고리의 다른 글
Python f-strings (Keep updating) (0) | 2020.04.04 |
---|---|
List comprehension (0) | 2020.03.30 |
International Morse code (di-dah) (0) | 2019.02.25 |
What is the point of float("-inf"), float("inf") (0) | 2019.01.22 |
Regular expression practice (0) | 2019.01.17 |