Skip to content

Instantly share code, notes, and snippets.

@kurzweil777
Last active May 20, 2020 06:22
Show Gist options
  • Save kurzweil777/3abef0c8dccb5692c3312792f5ab991c to your computer and use it in GitHub Desktop.
Save kurzweil777/3abef0c8dccb5692c3312792f5ab991c to your computer and use it in GitHub Desktop.
Regular Expressions
import re
numbers = ("12345678901,\n"
"12345678901,\n"
"34567990123,\n"
"45679901237,\n"
"46789012347,\n"
"17890123456,\n"
"99723877828,\n"
"13828392321\n")
numbersRegex = re.compile(r'''^1\d+''', re.MULTILINE) # Номера начинаются на 1
result = numbersRegex.findall(numbers)
numbersRegex2 = re.compile(r'''^7\d+''', re.MULTILINE) # Номера начинаются на 7
result2 = numbersRegex2.findall(numbers)
numbersRegex3 = re.compile(r'''^(1\d+1),?$''', re.MULTILINE) # Номера начинаются и заканчиваются на 1
result3 = numbersRegex3.findall(numbers)
numbersRegex4 = re.compile(r'''\d+99\d+|^99\d+|\d+99''', re.MULTILINE) # Номера содержат 99
result4 = numbersRegex4.findall(numbers)
print(result) # ['12345678901', '12345678901', '17890123456', '13828392321']
print(result2) # []
print(result3) # ['12345678901', '12345678901', '13828392321']
print(result4) # ['34567990123', '45679901237', '99723877828']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment