Найти - Пользователи
Полная версия: О регулярных выражениях
Начало » Python для новичков » О регулярных выражениях
1
vkopey
1.Как в строке найти все позиции (индексы первого символа и последнего) искомого текста?
2.В чем разница функций re.search re.match ?
Infernus
Из доков с python.org по модулю re:
1.
MatchObject.start()
MatchObject.end()

Return the indices of the start and end of the substring matched by group; group defaults to zero (meaning the whole matched substring). Return -1 if group exists but did not contribute to the match. For a match object m, and a group g that did contribute to the match, the substring matched by group g (equivalent to m.group(g)) is

m.string
Note that m.start(group) will equal m.end(group) if group matched a null string. For example, after m = re.search('b(c?)', ‘cba’), m.start(0) is 1, m.end(0) is 2, m.start(1) and m.end(1) are both 2, and m.start(2) raises an IndexError exception.
An example that will remove remove_this from email addresses:
>>> email = "tony@tiremove_thisger.net"
>>> m = re.search("remove_this", email)
>>> email[:m.start()] + email[m.end():]
'tony@tiger.net'
2. Оттуда же:
match checks for a match only at the beginning of the string, while search checks for a match anywhere in the string (this is what Perl does by default)
>>> re.match("c", "abcdef")  # No match
>>> re.search("c", "abcdef") # Match
<_sre.SRE_Match object at ...>
vkopey
1.Спасибо, но как же найти ВСЕ позиции? Надо организовать цикл?
2.А что такое группы в регулярных выражениях?
vkopey
Ответ нашел сам:
for mo in re.finditer(r"\#{2}.*?\#{2}", text):
print mo.span()
Infernus
Группы — это то, что в паттерне заключено в скобки ( ).
MatchObject.group(0) возвращает всё совпадение, MatchObject.group(i) — i-тую подгруппу совпадения.
>>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
>>> m.group(0) # The entire match
'Isaac Newton'
>>> m.group(1) # The first parenthesized subgroup.
'Isaac'
>>> m.group(2) # The second parenthesized subgroup.
'Newton'
>>> m.group(1, 2) # Multiple arguments give us a tuple.
('Isaac', 'Newton')
P.S. Читайте docs.python.org — там есть практически всё ;)
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB