Pattern extraction using Python regular expressions

Asked 1 years ago, Updated 1 years ago, 348 views

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

2023-02-22 12:31

1 Answers



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.~


2023-02-22 17:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.