代码之家  ›  专栏  ›  技术社区  ›  Aren Hovsepyan

如何在第一个met space char regex中分离匹配子字符串

  •  0
  • Aren Hovsepyan  · 技术社区  · 4 年前

    我需要在格式中匹配子字符串 !some text!https://test.com/ 两个之间的任何东西 ! 标记是文本,后面是没有空格的url,

    到目前为止,我做了一个正则表达式,可以找到子串 (!(.*)!((https|http):\/\/[^\s+]))

    例如

    !text goes here!https://amazon.com and some other text comes here 应该解析

    !text goes here!https://amazon.com

    它做得很好,但是当我在文本的任何地方复制这种模式时,它会匹配两个文本之间的整个文本。

    !text goes here!https://amazon.com some text here !text goes here!https://amazon.com and after some other text

    应该是两个分开的 !text goes here!https://amazon.com some 但它会选择子串直到第二次匹配结束

    !text goes here!https://amazon.com some text here !text goes here!https://amazon.com

    也许这需要整个文本 ! 最后呢 !

    当url文本后遇到空格时,是否有停止匹配的方法

    1 回复  |  直到 4 年前
        1
  •  2
  •   rici    4 年前

    !.*! 匹配由感叹号包围的可能最长的子串;换句话说,它从第一次爆炸到最后一次爆炸。

    !.*?! 或者更精确的匹配, ![^!]*!