regexp.compile(pattern, flags=0)
method is used to compile a regular expression pattern provided as a string into a regex pattern object ( re. Pattern ). Later we can use this pattern object to search for a match inside different target strings using regex methods such as a match() or search().
pattern = re.compile("oghggee")
print(pattern.fullmatch("oghggee"))
👉 Совпадениe <re.Match object; span=(0, 7), match='oghggee'>
print(pattern.fullmatch("ogre"))
👉 Совпадений нет т.к. совпала не вся строка None
print(pattern.fullmatch("oghggee", 4, 10))
👉 Совпадение нeт.поиск ограничен Noneprog = re.compile(r'(?i)[а-я]+')
print(prog)
👉 re.compile('(?i)[а-я]+', re.IGNORECASE)p = re.compile(r'\W+')
p.split('This is a test, short and sweet, of split().')
👉 ['This', 'is', 'a', 'test', 'short', 'and', 'sweet', 'of', 'split', '']
p.split('This is a test, short and sweet, of split().', 3)
👉 ['This', 'is', 'a', 'test, short and sweet, of split().']regexp.match(pattern, string, flags=0)
function checks the string to be matched for a pattern in the RegEx and returns the first occurrence of such a pattern match. This function only checks for a match at the beginning of the string.
print(re.match('super', 'superstition').span())
👉 (0, 5)
print(re.match('super', 'insuperable'))
👉 Nonefor x in range(int(input())) :
print(bool(re.match(r"^[+-]?[0-9]*\.[0-9]+$",input())))
👉 (^[+-]{1}\d*[.]{1}[0]*[1-9]*)m = re.match(r'(\w+)@(\w+)\.(\w+)','username@hackerrank.com')
m.groups()
👉 ('username', 'hackerrank', 'com')m = re.match(r'(?P<user>\w+)@(?P<website>\w+)\.(?P<extension>\w+)','myname@hackerrank.com')
m.groupdict()
👉 {'website': 'hackerrank', 'user': 'myname', 'extension': 'com'}regexp.search(pattern, string, flags=0)
сканирует строку string в поисках первого совпадения с шаблоном pattern регулярного выражения и возвращает соответствующий объект соответствия. В отличии от match()ищет везде, а не только в начале.
s, k = "aaadaa", "aa"
length = len(k)
pattern = f"{[k]}" + "{" + f"{length}" + "}"
match = re.search(pattern, s)print(re.search('super', 'superstition').span())
👉 (0, 5)
print(re.search('super', 'insuperable'))
👉 (2, 7)#Search for an upper case "S" character in the beginning of a word, and print the word: txt = "The rain in Spain" x = re.search(r"\bS\w+", txt)
#The string property returns the search string: txt = "The rain in Spain" x = re.search(r"\bS\w+", txt) print(x.string) 👉 The rain in Spain
#Search for an upper case "S" character in the beginning of a word, and print its position: txt = "The rain in Spain" x = re.search(r"\bS\w+", txt) print(x.span()) 👉 (12, 17)
print(re.search('super', 'insuperable'))
👉 (2, 7)s = input() m = re.search(r'([a-zA-Z0-9])\1', s) print(m) 👉 <re.Match object; span=(11, 13), match='11'> print(m.group(1)) 👉 1
regexp.VERBOSE
Without Using VERBOSE
позволяет писать регулярные выражения, которые выглядят лучше и удобнее для чтения, позволяя визуально разделять логические разделы шаблона и добавлять комментарии. Пробельные символы в шаблоне игнорируются, за исключением случаев, когда они находятся в символьном классе.
regex_email = re.compile(r'^([a-z0-9_\.-]+)@([0-9a-z\.-]+)\.([a-z\.]{2, 6})$',
re.IGNORECASE)
# Using VERBOSE
regex_email = re.compile(r"""
^([a-z0-9_\.-]+) # local Part
@ # single @ sign
([0-9a-z\.-]+) # Domain name
\. # single Dot .
([a-z]{2,6})$ # Top level Domain
""",re.VERBOSE | re.IGNORECASE)regexp.I or regexp.IGNORECASE
выполняет сопоставление без учета регистра. Выражения типа [A-Z] также будут соответствовать строчным буквам.
re
regexp.S or regexp.DOTALL
делает так, что бы специальный символ ‘.’ - точка соответствовал любому символу вообще, включая новую строку ‘\n’.
regexp.split(string [, maxsplit=0])
разбивает строку на части везде, где есть совпадения с регулярным выражением, указанным в качестве разделителя и возвращает список частей строки. Можно ограничить количество выполненных разбиений, передав значение maxsplit.
p = re.compile(r'\W+')
p.split('This is a test, short and sweet, of split().')
👉 ['This', 'is', 'a', 'test', 'short', 'and', 'sweet', 'of', 'split', '']
p.split('This is a test, short and sweet, of split().', 3)
👉 ['This', 'is', 'a', 'test, short and sweet, of split().']regexp.sub(pattern, repl, string, count=None)
is used to replace substrings in strings.
a_string = "abc xxx abc xxx"
new_string = re.sub("xxx", "abc", a_string, 1)
print(new_string)
👉 abc abc abc xxxa_string = "abc xxx abc yyy" new_string = re.sub(r"xxx|yyy", "abc", a_string) print(new_string) 👉 abc abc abc abc
digits_re = r'\d+'
sample = '/usr/sbin/sendmail - 0 errors, 12 warnings'
print(re.sub(digits_re, digits_re.replace('\\', r'\\'), sample))
👉 /usr/sbin/sendmail - \d+ errors, \d+ warningsregexp.fullmatch()
возвращает соответствующий объект сопоставления если вся строка соответствует скомпилированному регулярному выражению Pattern. Метод Pattern.fullmatch() вернет None если строка не соответствует шаблону.
pattern = re.compile("oghggee")
print(pattern.fullmatch("oghggee"))
👉 Совпадениe <re.Match object; span=(0, 7), match='oghggee'>
print(pattern.fullmatch("ogre"))
👉 Совпадений нет т.к. совпала не вся строка None
print(pattern.fullmatch("oghggee", 4, 10))
👉 Совпадение нeт.поиск ограничен Noneregexp.findall(pattern, string, flags=0)
Returns a list containing all matches. Строка сканируется слева направо, и совпадения возвращаются в найденном порядке. Если в шаблоне регулярного выражения присутствует одна или несколько групп, то findall() вернет список групп - это будет список кортежей, если шаблон содержит более одной группы. Пустые совпадения включаются в результат.
text = 'ул. Карпинского, дом № 20, корпус 3, квартира 98' match = re.findall(r'\d+', text) print(match) 👉 ['20', '3', '98']
text = 'ул. Карпинского, дом № 20, корпус 3, квартира 98' match = re.findall(r'(?i)[а-я]+', text) print(match) 👉 ['ул', 'Карпинского', 'дом', 'корпус', 'квартира']
regexp.finditer(pattern, string, flags=0)
возвращает итератор объектов сопоставления по всем неперекрывающимся совпадениям для шаблона регулярного выражения в строке.
text = 'ул. Карпинского, дом № 5, корпус 3, квартира 98' match = re.finditer(r'\d+', text) print(list(match)) # [ # <_sre.SRE_Match object; span=(25, 26), match='5'>, # <_sre.SRE_Match object; span=(34, 35), match='3'>, # <_sre.SRE_Match object; span=(45, 47), match='98'> # ]
regexp.match.group()
возвращает одну или несколько подгрупп совпадения. При наличии одного аргумента результатом будет одна строка, при наличии нескольких аргументов результатом будет кортеж с одним элементом на аргумент.
#Search for an upper case "S" character in the beginning of a word, and print the word: txt = "The rain in Spain" x = re.search(r"\bS\w+", txt) print(x.group()) 👉 Spain
regexp.search.string
string property returns the search string
#The string property returns the search string: txt = "The rain in Spain" x = re.search(r"\bS\w+", txt) print(x.string) 👉 The rain in Spain
regexp.search.span
Search for an character and print its position
print(re.match('super', 'superstition').span())
👉 (0, 5)print(re.search('super', 'superstition').span())
👉 (0, 5)regexp.subn
Perform the same operation as sub(), but return a tuple (new_string, number_of_subs_made).
regexp.escape
Escape special characters in pattern. This is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it.
regexp.groups()
expression returns a tuple containing all the subgroups of the match.
m = re.match(r’(\w+)@(\w+).(\w+)’,’username@hackerrank.com’)
m.groups()
👉 (‘username’, ‘hackerrank’, ‘com’)
regexp.groupdict()
expression returns a dictionary containing all the named subgroups of the match, keyed by the subgroup name.
m = re.match(r'(?P<user>\w+)@(?P<website>\w+)\.(?P<extension>\w+)','myname@hackerrank.com')
m.groupdict()
👉 {'website': 'hackerrank', 'user': 'myname', 'extension': 'com'}regexp.VERBOSE
позволяет писать регулярные выражения, которые выглядят лучше и удобнее для чтения, позволяя визуально разделять логические разделы шаблона и добавлять комментарии. Пробельные символы в шаблоне игнорируются, за исключением случаев, когда они находятся в символьном классе.
regex_email = re.compile(r'^([a-z0-9_\.-]+)@([0-9a-z\.-]+)\.([a-z\.]{2, 6})$', re.IGNORECASE)
# Using VERBOSE
regex_email = re.compile(r"""
^([a-z0-9_\.-]+) # local Part
@ # single @ sign
([0-9a-z\.-]+) # Domain name
\. # single Dot .
([a-z]{2,6})$ # Top level Domain
""",re.VERBOSE | re.IGNORECASE)regexp.I or regexp.IGNORECASE
выполняет сопоставление без учета регистра. Выражения типа [A-Z] также будут соответствовать строчным буквам.
prog = re.compile(r'(?i)[а-я]+')
print(prog)
👉 re.compile('(?i)[а-я]+', re.IGNORECASE)regex_name = re.compile(r'^(Mr\.|Mrs\.|Ms\.) ([a-z]+)( [a-z]+)*( [a-z]+)*$', re.IGNORECASE)
regexp.S or regexp.DOTALL
делает так, что бы специальный символ ‘.’ - точка соответствовал любому символу вообще, включая новую строку ‘\n’.
regexp.Any character (except newline character)
.
regexp.Returns a match where the string contains digits 👉 (0-9)
\d
regexp.Returns a match where the string DOES NOT contain digits 👉 (not 0-9)
\D