Python正则表达式匹配多行 [英] Python regex matching multiple lines

查看:65
本文介绍了Python正则表达式匹配多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Python 中的网页获取

    标记的内容.我使用了以下代码:
    matchResult = re.search(r'<ul\s*content="MSL">(.*)</ul>', queryResult, re.MULTILINE)
    这不起作用.但是,如果我使用
    删除换行符queryResult = queryResult.replace('\r','').replace('\n','')
    它有效.

    I'm trying to get the contents of a <ul> tag from a webpage in python. I used the following code:
    matchResult = re.search(r'<ul\s*content="MSL">(.*)</ul>', queryResult, re.MULTILINE)
    This does not work. However if I remove the line breaks by using
    queryResult = queryResult.replace('\r','').replace('\n','')
    It works.

    PHP 中的这个正则表达式在不删除换行符的情况下工作正常:preg_match('@<ul\s*content="MSL">(.*)</ul>@msU', $queryResult, $matches);

    This regular expression in PHP works fine without removing line breaks: preg_match('@<ul\s*content="MSL">(.*)</ul>@msU', $queryResult, $matches);

    如何使用 Python 匹配多行?

    How can I match over multiple lines using Python?

    推荐答案

    还包括 re.DOTALL 选项,这将允许 . 字符匹配换行符.

    Include the re.DOTALL option as well, that will allow the . character to match newlines.

    这篇关于Python正则表达式匹配多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆