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

Python 当存在标记时查找定位文本

Python 当存在标记时查找定位文本,python,regex,Python,Regex,我想在一对“”之间查找文本 但那只是在链接之后页面上的所有内容,我不想要。有什么建议可以帮助我解决这个问题吗?我不会使用正则表达式-使用HTML解析器,比如 (.*?) 而不是: [^<>]* [^]* 尝试: ((?!>>>导入re) >>>pattern=re.compile(r'',re.IGNORECASE) >>>链接=“” >>>重新匹配(模式、链接)。组(1) 'http://stackoverflow.com/questions/603199/finding-

我想在一对“”之间查找文本
但那只是在链接之后页面上的所有内容,我不想要。有什么建议可以帮助我解决这个问题吗?

我不会使用正则表达式-使用HTML解析器,比如

(.*?)
而不是:

[^<>]*
[^]*
尝试:

((?!
>>>导入re)
>>>pattern=re.compile(r'',re.IGNORECASE)
>>>链接=“”
>>>重新匹配(模式、链接)。组(1)
'http://stackoverflow.com/questions/603199/finding-anchor-text-when-there-are-tags-there'
>>>重新匹配(模式、链接)。组(2)
'当存在标记时查找定位文本'

它只在锚文本中的标记对于这样一个简单的问题来说似乎有点重之前才匹配。HTML是高度不规则的——浏览器需要容忍大量错误。Beauty Soup可以比regexes更好地处理不规则HTML。非常感谢您的帮助:)谢谢,这对我帮助很大。
([^<]*))</a>'''
(.*))</a>'''
(.*?)
[^<>]*
((?!</a).)*
>>> import re
>>> pattern = re.compile(r'<a.+href=[\'|\"](.+)[\'|\"].*?>(.+)</a>', re.IGNORECASE)
>>> link = '<a href="http://stackoverflow.com/questions/603199/finding-anchor-text-when-there-are-tags-there">Finding anchor text when there are tags there</a>'
>>> re.match(pattern, link).group(1)
'http://stackoverflow.com/questions/603199/finding-anchor-text-when-there-are-tags-there'
>>> re.match(pattern, link).group(2)
'Finding anchor text when there are tags there'