如何使用正则表达式来抓句子,避免使用标签

时间:2014-02-24 03:22:54

标签: python regex

我对表格有很多疑问:

<iiiihhiii? (end of line )

我想确保避免使用HTML标记。

我尝试了一个正则表达式:

<[^>]+\?

使用http://www.pythonregex.com/

这是输出:

>>> regex = re.compile("<[^>]+/?",re.UNICODE|re.DOTALL|re.VERBOSE)
>>> r = regex.search(string)
>>> r
<_sre.SRE_Match object at 0x87e2915436c23d50>
>>> regex.match(string)
<_sre.SRE_Match object at 0x87e2915436c23da8>    

# List the groups found
>>> r.groups()
()    

# List the named dictionary objects found
>>> r.groupdict()
{}    

# Run findall
>>> regex.findall(string)
[u'<jghjhgjhgjh?']

没有匹配发生。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您的正则表达式正确地从<开始,但[^>]匹配不会停留在?标记处,也不会停留在换行符上,因此它会继续匹配,直到达到> {1}}角色。也许可以尝试将其更新为<[^>\n?]+\?,以便它匹配除>,换行符或?问号之外的任何内容,然后当它遇到该跟踪问号时,您将其与{明确地{1}}。

答案 1 :(得分:0)

这对你有用吗?

<[^>]+[?]