--> Blockquote --> """ 我使用这个正则表达式 rege-6ren">
gpt4 book ai didi

python - sre_constants.error : nothing to repeat in jython

转载 作者:行者123 更新时间:2023-12-04 14:56:41 25 4
gpt4 key购买 nike

我有html内容我想从这个内容中获得评论

content = """<html>
<body>
<!--<h1>test</h1>-->
<!--<div>
<img src='x'>
</div>-->

Blockquote

<!--
<div>
<img src='xe'>
</div>
-->
</body>
</html>"""

我使用这个正则表达式

regex_str = "<!--((\n|\r)+)?((.*?)+((\n|\r)+)?)+-->"

Python 中运行此行时

re.findall(regex_str,content)

成功运行

但是在 jython 中运行时出现这个错误

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/share/java/jython-2.7.2.jar/Lib/re$py.class", line 177, in findall
File "/usr/share/java/jython-2.7.2.jar/Lib/re$py.class", line 242, in _compile
sre_constants.error: nothing to repeat

最佳答案

使用

<!--[\n\r]*([\w\W]*?)[\n\r]*-->

参见 regex proof .

解释

--------------------------------------------------------------------------------
<!-- '<!--'
--------------------------------------------------------------------------------
[\n\r]* any character of: '\n' (newline), '\r'
(carriage return) (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[\w\W]*? any character of: word characters (a-z,
A-Z, 0-9, _), non-word characters (all
but a-z, A-Z, 0-9, _) (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
[\n\r]* any character of: '\n' (newline), '\r'
(carriage return) (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
--> '-->'

关于python - sre_constants.error : nothing to repeat in jython,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67828254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com