【问题标题】:Unable to find the regex for this pattern找不到此模式的正则表达式
【发布时间】:2021-10-12 03:24:06
【问题描述】:

对于 dotnet 应用程序,我有一个字符串模式,如下所示:

TEAM AND CO TEAM
TESTER
Name anderson rahul alice

我只需要捕获“Name”的值,但是只有当字符串序列“TEAM AND CO TEAM”在一行,“TESTER”在下一行,“名称”出现在下一行的旁边。

下面是我的正则表达式,但它也选择了第二次出现的名称,我不需要:

\bName\s+(.*)$

输入字符串:

TEAM AND CO TEAM
TESTER
Name anderson rahul alice

LTest SYNC DIM TESTER
PHYSICS
Name KIM SID andy

任何帮助都将是可观的。

Screenshot of the regex

【问题讨论】:

    标签: c# asp.net regex


    【解决方案1】:

    使用

    (?m)(?<=^TEAM AND CO TEAM\r?\nTESTER\r?\nName\s).+
    

    regex proof

    解释

    --------------------------------------------------------------------------------
      (?m)                     set flags for this block (with ^ and $
                               matching start and end of line) (case-
                               sensitive) (with . not matching \n)
                               (matching whitespace and # normally)
    --------------------------------------------------------------------------------
      (?<=                     look behind to see if there is:
    --------------------------------------------------------------------------------
        ^                        the beginning of a "line"
    --------------------------------------------------------------------------------
        TEAM AND CO TEAM         'TEAM AND CO TEAM'
    --------------------------------------------------------------------------------
        \r?                      '\r' (carriage return) (optional
                                 (matching the most amount possible))
    --------------------------------------------------------------------------------
        \n                       '\n' (newline)
    --------------------------------------------------------------------------------
        TESTER                   'TESTER'
    --------------------------------------------------------------------------------
        \r?                      '\r' (carriage return) (optional
                                 (matching the most amount possible))
    --------------------------------------------------------------------------------
        \n                       '\n' (newline)
    --------------------------------------------------------------------------------
        Name                     'Name'
    --------------------------------------------------------------------------------
        \s                       whitespace (\n, \r, \t, \f, and " ")
    --------------------------------------------------------------------------------
      )                        end of look-behind
    --------------------------------------------------------------------------------
      .+                       any character except \n (1 or more times
                               (matching the most amount possible))
    

    【讨论】:

      【解决方案2】:

      在您的模式 \bName\s+(.*)$ 中,您只匹配 Name,然后在匹配 1 个或多个空白字符后捕获该行的其余部分。

      您可以扩展模式以匹配下一行中所需的行TEAM AND CO TEAMTESTER

      然后匹配 Name 并在第 1 组中捕获您想要的值。

      注意启用多行标志。

      ^TEAM AND CO TEAM\r?\nTESTER\r?\nName\s(.+)
      

      说明

      • ^ 字符串开始
      • TEAM AND CO TEAM\r?\n 匹配 TEAM AND CO TEAM,可选回车符和换行符
      • TESTER\r?\n 匹配 TESTER,可选回车符和换行符
      • Name\s 匹配 Name 和一个空白字符
      • (.+) 捕获第 1 组,匹配 1 个或多个将匹配行的其余部分的字符

      查看.NET regex demo

      【讨论】:

        猜你喜欢
        • 2017-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多