Home > Blockchain >  Regex help requested first words in 2 consecutive lines
Regex help requested first words in 2 consecutive lines

Time:01-24

I need a regex for the following sample text:

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

The regex must result "Doe" (which could be any other name or word) based on fixed "Name" string in the line below it. Both words are first in line.

This regex gives me every first word of every line, but need help with the check on "Name":

(^\w )

CodePudding user response:

Use

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

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  ^                        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

CodePudding user response:

Try:

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

Poul Bak Jan 20 at 23:50

  •  Tags:  
  • Related