regex 正则表达式帮助要求连续2行中的第一个单词

hfyxw5xn  于 2022-11-18  发布在  其他
关注(0)|答案(2)|浏览(119)

我需要一个正则表达式为以下示例文本:

Doe printing and typesetting industry.
Name Ipsum has been the industry's standard.

正则表达式必须根据其下一行中的固定“Name”字符串得出“Doe”(可以是任何其他名称或单词)。这两个单词都是该行的第一个单词。
这个正则表达式给我每一行的第一个单词,但需要帮助检查“名称”:

(^\w+)
ajsxfq5m

ajsxfq5m1#

用途

^(\S+)(?=.*\r?\nName\b)

请参阅regex proof

说明

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    \S+                      non-whitespace (all but \n, \r, \t, \f,
                             and " ") (1 or more times (matching the
                             most amount possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    \r?                      '\r' (carriage return) (optional
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    \n                       '\n' (newline)
--------------------------------------------------------------------------------
    Name                     'Name'
--------------------------------------------------------------------------------
    \b                       the boundary between a word char (\w)
                             and something that is not a word char
--------------------------------------------------------------------------------
  )                        end of look-ahead
q7solyqu

q7solyqu2#

请尝试:

(^\w+)(?=.*\nName)

保罗巴克1月20日23:50

相关问题