【问题标题】:ReGex, How to find second instance of string正则表达式,如何找到字符串的第二个实例
【发布时间】:2020-12-10 18:24:22
【问题描述】:

如果我想获得介于“for”和“;”之间的名称这是NISHER HOSE,你能帮我找到正确的正则表达式吗,因为“for”和“;”不止一个在字符串中

访问请求需要数据所有者批准 #:NISHER HOSE 的 2137352;承包商;经理:MUILLER, TIM (TWM0069)

使用正则表达式 (?<=for).*(?=;) 我得到了错误的匹配 Access Request #: 2137352 for NISHER HOSE; CONTRACTOR - 请参阅 screenshot on https://www.regextester.com/

谢谢

【问题讨论】:

  • 试试 (?

标签: regex regex-greedy


【解决方案1】:

如果您只想在左侧断言for ,您应该并确保不再匹配for并且您应该排除匹配;,同时断言在右边。

(?<=\bfor )(?:(?!\bfor\b)[^;])+(?=;)

说明

  • (?&lt;=\bfor ) 在左边断言for
  • (?:(?!\bfor\b)[^;])! 匹配除 ; 之外的任何字符的 1 次以上,如果从当前位置开始不直接后跟 for,则被单词边界包围
  • (?=;) 直接在右边断言;

Regex demo

【讨论】:

  • 完美解决了我的问题!谢谢
【解决方案2】:

使用

(?<=\bfor )(?![^;]*\bfor\b)[^;]+

proof

说明

--------------------------------------------------------------------------------
  (?<=                     look behind to see if there is:
--------------------------------------------------------------------------------
    \b                       the boundary between a word char (\w)
                             and something that is not a word char
--------------------------------------------------------------------------------
    for                      'for '
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    [^;]*                    any character except: ';' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \b                       the boundary between a word char (\w)
                             and something that is not a word char
--------------------------------------------------------------------------------
    for                      'for'
--------------------------------------------------------------------------------
    \b                       the boundary between a word char (\w)
                             and something that is not a word char
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  [^;]+                    any character except: ';' (1 or more times
                           (matching the most amount possible))

【讨论】:

    【解决方案3】:

    这里的主要问题是有两个“for”。如果要捕获名称,请使用“:”作为分隔符来捕获第二个“for”:

    名称将在第 1 组中捕获。如果您决定使用前瞻/后瞻,请记住,根据正则表达式引擎,这些可能受支持也可能不支持。

    【讨论】:

    • 谢谢。从这里我必须使用字符串操作来获取名称吗?否则,我怎么能从这里得到名字。
    • 正则表达式匹配返回匹配和任何捕获的组。在这种情况下,您可以在组 1 中找到名称。访问它取决于语言,但通常只需 match[1]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 2018-05-24
    相关资源
    最近更新 更多