Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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 如何用一个regex命令解析多行?_Python_Regex_Parsing - Fatal编程技术网

Python 如何用一个regex命令解析多行?

Python 如何用一个regex命令解析多行?,python,regex,parsing,Python,Regex,Parsing,我有两条线看起来像 Content-Type: text/plain Content-Type: text/plain; charset=UTF-8 为了进行分析,我使用了类似于(“^Content Type:\s(.*))的命令来捕获(text/plain)部分。另一方面,我使用类似regex的(“^Content Type:\s(.*)[;]”)来捕获相同的字符串(text/plain)。有没有什么方法可以让我使用一个在这两种情况下都有效的方法?我正在使用python,而且我对正则表达式是

我有两条线看起来像

Content-Type: text/plain
Content-Type: text/plain; charset=UTF-8

为了进行分析,我使用了类似于
(“^Content Type:\s(.*))
的命令来捕获
(text/plain)
部分。另一方面,我使用类似regex的
(“^Content Type:\s(.*)[;]”)来捕获相同的字符串
(text/plain)
。有没有什么方法可以让我使用一个在这两种情况下都有效的方法?我正在使用python,而且我对正则表达式是新手。谢谢

您可以稍微修改一下您的正则表达式:

Content-Type:\s([^;\s]*)

这里有一个工作链接:

您可以稍微修改一下您的正则表达式:

Content-Type:\s([^;\s]*)

这里有一个工作链接:

看起来您正在寻找
量词(在列表中排名第6)。它将允许尾随部分出现一次或根本不出现,涵盖两种情况:

^Content-Type:\s+([^;]+)(?:;.*)?
看起来您正在查找
量词(在列表中排名第6)。它将允许尾随部分出现一次或根本不出现,涵盖两种情况:

^Content-Type:\s+([^;]+)(?:;.*)?
正如我在评论中所说的,对于这样一个简单的匹配,正则表达式是一种过分的技巧,因此为了完整性:

def parse_content_type(data):
    if data.lower()[:13] == "content-type:":  # HTTP headers are case-insensitive by spec.
        index = data.find(";")  # find the position of `;`
        return data[13:index if index > -1 else len(data)].strip()  # slice and strip

print(parse_content_type("Content-Type: text/plain"))  # text/plain
print(parse_content_type("Content-Type: text/plain; charset=UTF-8"))  # text/plain

它更详细,但理论上应该更快。

正如我在评论中所说的,对于这样一个简单的匹配来说,正则表达式是一种过分的技巧,因此为了完整性:

def parse_content_type(data):
    if data.lower()[:13] == "content-type:":  # HTTP headers are case-insensitive by spec.
        index = data.find(";")  # find the position of `;`
        return data[13:index if index > -1 else len(data)].strip()  # slice and strip

print(parse_content_type("Content-Type: text/plain"))  # text/plain
print(parse_content_type("Content-Type: text/plain; charset=UTF-8"))  # text/plain

它更详细,但理论上应该更快。

标记应该告知用户您的语言。这次我在中为您编辑了它。
^内容类型:\s+(.*?(=>;|$)
尽管对于这样一个简单的情况,您根本不需要正则表达式。标记应该会通知用户您的语言。这次我为您编辑了它。
^内容类型:\s+(.*)(=>;$)
尽管对于这样一个简单的情况,您根本不需要正则表达式。OP试图捕获内容类型字符串,而不是整个标题行:
text/plain
,而不是
内容类型:text/plain
。OP试图捕获内容类型字符串,不是整个标题行:
text/plain
,不是
内容类型:text/plain