【问题标题】:sre_constants.error: nothing to repeat in jythonsre_constants.error:在 jython 中没有可重复的内容
【发布时间】:2021-06-03 20:25:46
【问题描述】:

我有 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

【问题讨论】:

  • 您是否尝试过双重转义反斜杠?
  • 你的意思是(\\.*) 对吗?
  • \\r\\n 请注意,您可以将其写为[\n\r] 或双重转义[\\r\\n] 或Java 中的\\R
  • 问题出在这部分,我想(.*?)+我已经尝试过了,但同样的问题

标签: python regex jython


【解决方案1】:

使用

<!--[\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))
--------------------------------------------------------------------------------
  -->                      '-->'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-03
    • 2018-05-21
    • 2020-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-03
    相关资源
    最近更新 更多