Home > OS >  Regex Find firstname based on
Regex Find firstname based on

Time:01-29

Need some help with the firstname match based on search "Name" below the first line. Below 3 situations that can occur:

Mrs. Joanne Doe
Name detail

Jenni Doe
Name detail

Mr. John Doe
Name detail

This will solve the 2nd situation (^\w )(?=.*\nName). I need 1 regex that will cope with all the 3 scenario's.

Thanks!

CodePudding user response:

SOLVED By Wiktor with:

^(?:M(?:rs?|(?:is)?s)\.?\s*)?\K\w (?=.*\nName)

Demo

CodePudding user response:

You can use

^(?:M(?:rs?|(?:is)?s)\.?\s*)?\K\w (?=.*\RName)

See the regex demo. Details:

  • ^ - start of string
  • (?:M(?:rs?|(?:is)?s)\.?\s*)? - an optional occurrence of Mr, Mrs, Miss or Ms optionally followed with a . char and then zero or more whitespaces
  • \K - omit all text matched so far
  • \w - one or more letters, digits or underscores
  • (?=.*\RName) - a positive lookahead that requires any zero or more chars other than line break chars as many as possible, a line break sequence and then Name string immediately to the right of the current position.
  •  Tags:  
  • Related