ReGex,如何找到字符串的第二个实例

ReGex, How to find second instance of string

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

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

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

谢谢

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

名称将在第 1 组中捕获。如果您决定使用 lookahead/lookbehind 请记住,这些名称可能受支持也可能不受支持,具体取决于正则表达式引擎。

如果你只想在左边断言 for ,你应该并确保不再匹配 for 你应该排除匹配; 同时断言右边有一个。

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

说明

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

Regex demo

使用

(?<=\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))