You want to extract a specific pattern using Python's regular expression. (It doesn't matter if there's a better way than regular expression)
For example, -> Hello. Today is (12.05.26). 789261-12.
A number such as 12.05.26 to obtain only the date when the sentence is like the above.Numbers. Numbers. I want to extract the pattern. How do I make the code?
python regex
import re
give_str = 'Hello, today is (12.05.26). 789261-12.'
bracket_pattern = r'\(([^)]+)'
matched_strs = re.findall(bracket_pattern, given_str)
print(matched_strs)
date_pattern = r'\d{2}.\d{2}.\d{2}'
for matched_str in matched_strs:
if bool(re.match(date_pattern, matched_str)) == True:
my_date = matched_str
break
print(my_date)
You can apply it like this.~
© 2024 OneMinuteCode. All rights reserved.