Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 要匹配的正则表达式,直到满足特定字符串或字符串结尾_Python_Regex_Regex Lookarounds - Fatal编程技术网

Python 要匹配的正则表达式,直到满足特定字符串或字符串结尾

Python 要匹配的正则表达式,直到满足特定字符串或字符串结尾,python,regex,regex-lookarounds,Python,Regex,Regex Lookarounds,我正在尝试创建正确的正则表达式,以便在python中用于以下场景的多行匹配。我需要在匹配字符串描述\s:后跳过一行,然后获取第一次出现\s之前的所有文本。\n或字符串主页:或字符串结尾 我正在尝试下面的正则表达式,但是缺少了一些东西,并且没有涵盖所有的场景:Description\s*:\s*\n(?p[\w\s\$\&+\,\:\;\=\?\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\%!\]*\n\s*。 情景1: 预期结果:“libX11 xcb提供了利用 Xli

我正在尝试创建正确的正则表达式,以便在python中用于以下场景的多行匹配。我需要在匹配字符串描述\s:后跳过一行,然后获取第一次出现\s之前的所有文本。\n或字符串主页:或字符串结尾

我正在尝试下面的正则表达式,但是缺少了一些东西,并且没有涵盖所有的场景:
Description\s*:\s*\n(?p[\w\s\$\&+\,\:\;\=\?\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\%!\]*\n\s*。

情景1: 预期结果:“libX11 xcb提供了利用 Xlib/XCB通过同一个X连接混合调用Xlib和XCB。“

如果能得到正确的表达方式,我们将不胜感激。

这应该行得通

import re
match = re.search(r'Description:.*?\n(.*?)(\s.\n|$)', str1, re.DOTALL)
print(match.group(1))

作为替代方案,您也可以不使用re.DOTALL来匹配所有不以空格和点开头的行,也可以使用负前瞻来防止不必要的回溯,使用
*?

注意:转义点
\。
以逐字匹配

\bDescription:.*\r?\n(?P<description>(?:(?! \.|$|Homepage).*(?:\r?\n)?)*)
\b说明:.*\r?\n(?P(?(?!\。$$主页)。*(?:\r?\n)?)*)
部分:

  • \b说明:.*\r?\n
    匹配说明:以及行的其余部分和换行符
  • (?P
    命名组描述
    • (?:
      非捕获组
      • (?!\.$|主页)
        断言直接右侧的内容不是备选方案之一
      • *(?:\r?\n)?
        匹配除换行符外的任何字符0+次,并匹配可选换行符
    • )*
      关闭非捕获组并重复0多次
  • 关闭第1组

我不懂Python,但我知道Matthew Barnett为Python创建了一个向后兼容
re
模块的版本。它的一个特性是支持来自PCRE(PHP)和Perl的
\K
。松散地说,
K
意味着忘记迄今为止匹配的所有内容。这样,就可以在不使用捕获组的情况下提取所需字符串:
说明:[^\r\n]*\r?\n\s*\K.*(?:\。\r?\n)
Architecture: blob
Multi-Arch: same
Recommends: ca-certificates
Description: easy-to-use client-side URL transfer library (OpenSSL flavour)
 libcurl is an easy-to-use client-side URL transfer library, supporting DICT,
 FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S,
 RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP.
 .
 libcurl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP
 form based upload, proxies, cookies, user+password authentication (Basic,
 Digest, NTLM, Negotiate, Kerberos), file transfer resume, http proxy tunneling
 and more!
 .
 libcurl is free, thread-safe, IPv6 compatible, feature rich, well supported,
 fast, thoroughly documented and is already used by many known, big and
 successful companies and numerous applications.
 .
 SSL support is provided by OpenSSL.
Homepage: http://curl.haxx.se
import re
match = re.search(r'Description:.*?\n(.*?)(\s.\n|$)', str1, re.DOTALL)
print(match.group(1))
\bDescription:.*\r?\n(?P<description>(?:(?! \.|$|Homepage).*(?:\r?\n)?)*)