【问题标题】:RegEx for capturing contents and ignoring newlines [duplicate]用于捕获内容并忽略换行符的正则表达式 [重复]
【发布时间】:2019-10-18 11:59:16
【问题描述】:

这是代码:

<!-- message --> 
<div><b><font size="6"><font color="Red">Bilim ve Teknik dergisi Mayıs 2019 Sayısı Pdf</font></font></b><br />
<br />
<img src="https://scontent-dus1-1.xx.fbcdn.net/v/t1.0-9/59069640_871111339894885_8805863518755618816_n.jpg?_nc_cat=109&amp;_nc_ht=scontent-dus1-1.xx&amp;oh=2a71d0bc34cda6b45404c30624c75046&amp;oe=5D6C1B30" border="0" alt="" /><br />
<br />
<b><font size="5"><a href="https://yadi.sk/i/oMnXUgBtTqKopg?fbclid=IwAR3KPXInlWCKFXuTKP1AU1VQGdsgvcDLdV9Px6YGOn3aU1tqAFz4Zo2J6PY" target="_blank">https://yadi.sk/i/oMnXUgBtTqKopg?fbc...1tqAFz4Zo2J6PY</a></font></b></div>
<!-- / message -->

如何在&lt;!-- message --&gt;&lt;!-- message --&gt; 之间切换?

我正在使用 Python 3 和 BeautifulSoup4。 以下代码产生空的混乱值:

tl="58421"
topLink="https://www.eskikitaplarim.com/showthread.php?t="+tl
page=s.get(topLink)
psoup=bs(page.text,'html.parser')
mess=psoup.find_all(text=re.compile("<!-- message -->(.*?)<!-- \/ message -->"))
print(mess)

【问题讨论】:

  • 我不知道为什么这被标记为重复。用户正在使用 BeautifulSoup,所以看起来他们可能想做的不仅仅是只是在 cmets 之间获取div。因此,可能不希望使用直接的正则表达式,因为他们可能想要进行额外的解析。由于该问题现在无法发布新答案,因此这里有一个适用于 BeautifulSoup 的问题。 pastebin.com/kn4BtYpi.

标签: regex beautifulsoup regex-lookarounds regex-group regex-greedy


【解决方案1】:

在这里,我们可以使用传递新行的表达式来获得所需的输出,例如:

<!-- message -->([\s\S]*)<!-- \/ message -->

Demo 1

或:

<!-- message -->([\d\D]*)<!-- \/ message -->
<!-- message -->([\w\W]*)<!-- \/ message -->

测试

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"<!-- message -->([\s\S]*)<!-- \/ message -->"

test_str = ("<!-- message --> \n"
    "<div><b><font size=\"6\"><font color=\"Red\">Bilim ve Teknik dergisi Mayıs 2019 Sayısı Pdf</font></font></b><br />\n"
    "<br />\n"
    "<img src=\"https://scontent-dus1-1.xx.fbcdn.net/v/t1.0-9/59069640_871111339894885_8805863518755618816_n.jpg?_nc_cat=109&amp;_nc_ht=scontent-dus1-1.xx&amp;oh=2a71d0bc34cda6b45404c30624c75046&amp;oe=5D6C1B30\" border=\"0\" alt=\"\" /><br />\n"
    "<br />\n"
    "<b><font size=\"5\"><a href=\"https://yadi.sk/i/oMnXUgBtTqKopg?fbclid=IwAR3KPXInlWCKFXuTKP1AU1VQGdsgvcDLdV9Px6YGOn3aU1tqAFz4Zo2J6PY\" target=\"_blank\">https://yadi.sk/i/oMnXUgBtTqKopg?fbc...1tqAFz4Zo2J6PY</a></font></b></div>\n"
    "<!-- / message -->")

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):

    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1

        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

正则表达式电路

jex.im 可视化正则表达式:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-13
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多