Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.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_Non Greedy - Fatal编程技术网

python正则表达式非贪婪强制

python正则表达式非贪婪强制,python,regex,non-greedy,Python,Regex,Non Greedy,我在这里看到了一个关于非贪婪匹配的示例。 reg_string = "(.*?)>Title" path = "<html><head><title>Title</title>" match = re.match(reg_string, path) if match: print match.group() 您可能希望查看BeautifulSoup Python库,以更直观地解析和处理HTML: 试试reg_string=“([^>]

我在这里看到了一个关于非贪婪匹配的示例。

reg_string = "(.*?)>Title"
path = "<html><head><title>Title</title>"
match = re.match(reg_string, path)
if match:
   print match.group()

您可能希望查看BeautifulSoup Python库,以更直观地解析和处理HTML:


试试
reg_string=“([^>]*?)>Title”
据我所知,您想在Title之前获取所有内容;但是如果没有标题文本,那么它应该抱怨吗

# Here we add a zero-to-many length match, delimited by `<` or end of line
# and capture it in a second group
reg_string = "(.*?)>(.*?)(<|$)"

path = "<html><head><title>Title</title>"

match = re.match(reg_string, path)
if match:
    if match.group(2) == "":
        throw Exception("No title content")
    else
        print match.group(1)
else:
    throw Exception("No match")

#这里我们添加一个零到多长度匹配,当“Title”不在第一个“>”之后时,由` It should complaint'分隔。然后您的主要示例就可以工作了。只需添加一个“else”子句,如果不匹配,则执行
非常有效!感谢you@EdaJede如果回答了您的问题,请将此答案标记为正确。
# Here we add a zero-to-many length match, delimited by `<` or end of line
# and capture it in a second group
reg_string = "(.*?)>(.*?)(<|$)"

path = "<html><head><title>Title</title>"

match = re.match(reg_string, path)
if match:
    if match.group(2) == "":
        throw Exception("No title content")
    else
        print match.group(1)
else:
    throw Exception("No match")