Найти - Пользователи
Полная версия: помогите с re
Начало » Python для новичков » помогите с re
1 2
sp3
всем здравствуйте.
помогите написать регулярку.
нужно найти строчку: Inserts a command to the server queue at the beginning and forces execution.


import re
text ='''
<p><script type="text/javascript"> if (window.showTocToggle) { var tocShowText = "show"; var tocHideText = "hide"; showTocToggle(); } </script>
</p><p><br />
</p>
<div class="editsection" style="float:right;margin-left:5px;">[<a href="/w/Template:Method/edit?section=4" title="Template:Method">edit</a>]</div><a name="Description"></a><h3>Description</h3>
<p>Inserts a command to the server queue at the beginning and forces execution.
</p>
<div class="editsection" style="float:right;margin-left:5px;">[<a href="/w/Template:Method/edit?section=5" title="Template:Method">edit</a>]</div><a name="Arguments"></a><h3>Arguments</h3>
<p><br />
</p>
'''

print re.findall(r"Description</h3>(.*?)</p>", text)
Vader
re.search('Inserts a command to the server queue at the beginning and forces execution.', text)
PooH
А регулярка то тут зачем? Просто ищите строчку.
Alex2ndr
Мне кажется что sp3 нужно найти эту строку, не зная ее. Т е вычистить из файла теги.
Nik
print re.findall(r"Description</h3>\s*<p[^>]*>(.*?)</p>", text, re.S | re.I)
o7412369815963
print re.findall(r"Description</h3>\n<p>(.*?)</p>", text, re.DOTALL)
или так можно попробовать (зависит от вида переноса строки)
print re.findall(r"Description</h3>\s+<p>(.*?)</p>", text, re.DOTALL)
o7412369815963
Nik
print re.findall(r"Description</h3>\s*<p[^>]*>(.*?)</p>", text, re.S | re.I)
а зачем "<p*>“ , там же ”<p>" - 3 символа вместе
Nik
а зачем "<p*>“ , там же ”<p>" - 3 символа вместе
Открывающие теги могут иметь (или не иметь) параметры… Вы же пытаетесь решить конкретную ситуацию, а не предвидеть возможные варианты…
sp3
Nik,o7412369815963 спасибо, очень помогли :)
o7412369815963
Nik
Вы же пытаетесь решить конкретную ситуацию
так и есть, т.к. это статический шаблон, изменяется только текст.

в регепс нужно ещё добавить флаг re.DOTALL (он же re.S), для того что-б получить многострочный текст,
без него регепс “(.*?)” вернет текст только до переноса строки, т.е. только первую строку
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