Last active
September 10, 2019 18:38
-
-
Save skonik/357ba17a44bb0dd78a9dc39c59ec2168 to your computer and use it in GitHub Desktop.
Python regex example (RU)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
text = "ГОСТ Р 54384-2011 - " \ | |
"Сталь. Определение и классификация по химическому составу и ... " \ | |
"ГОСТ 380-94 Сталь углеродистая обыкновенного качества." | |
gost_pattern = r'(?P<standard_type>гост)\s*(?P<standard_value>[рp]?\s*\d*\d+(?:-\d+)?)' | |
print(re.findall(gost_pattern, text, re.U + re.I)) | |
# [('ГОСТ', 'Р 54384-2011'), ('ГОСТ', '380-94')] | |
for match_obj in re.finditer(gost_pattern, text, re.U + re.I): | |
print(f'standard_type: {match_obj.group("standard_type")}, ' | |
f'standard_value: {match_obj.group("standard_value")}') | |
# standard_type: ГОСТ,standard_value: Р 54384-2011 | |
# standard_type: ГОСТ,standard_value: 380-94 | |
gost_pattern_search = re.search(gost_pattern, text, re.U + re.I) | |
if gost_pattern_search: | |
print(gost_pattern_search.group()) | |
# standard_type: ГОСТ,standard_value: 380-94 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment