Уведомления

Группа в Telegram: @pythonsu

#1 Авг. 10, 2011 22:38:23

vkopey
От:
Зарегистрирован: 2010-09-26
Сообщения: 116
Репутация: +  3  -
Профиль   Отправить e-mail  

О регулярных выражениях

1.Как в строке найти все позиции (индексы первого символа и последнего) искомого текста?
2.В чем разница функций re.search re.match ?



Офлайн

#2 Авг. 10, 2011 23:08:30

Infernus
От:
Зарегистрирован: 2011-06-04
Сообщения: 27
Репутация: +  0  -
Профиль   Отправить e-mail  

О регулярных выражениях

Из доков с 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 ...>



Отредактировано (Авг. 10, 2011 23:09:11)

Офлайн

#3 Авг. 10, 2011 23:24:41

vkopey
От:
Зарегистрирован: 2010-09-26
Сообщения: 116
Репутация: +  3  -
Профиль   Отправить e-mail  

О регулярных выражениях

1.Спасибо, но как же найти ВСЕ позиции? Надо организовать цикл?
2.А что такое группы в регулярных выражениях?



Офлайн

#4 Авг. 10, 2011 23:41:34

vkopey
От:
Зарегистрирован: 2010-09-26
Сообщения: 116
Репутация: +  3  -
Профиль   Отправить e-mail  

О регулярных выражениях

Ответ нашел сам:

for mo in re.finditer(r"\#{2}.*?\#{2}", text):
print mo.span()



Офлайн

#5 Авг. 11, 2011 08:44:32

Infernus
От:
Зарегистрирован: 2011-06-04
Сообщения: 27
Репутация: +  0  -
Профиль   Отправить e-mail  

О регулярных выражениях

Группы — это то, что в паттерне заключено в скобки ( ).
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 — там есть практически всё ;)



Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version