Home > Software design >  Regex matches specific occurences
Regex matches specific occurences

Time:12-14

I am trying to use regex (PCRE2) to scrape data from mail, I am having issue trying to match specific occurrence.

Example:

Person Requesting

Salutation Mr.
First Name* Paul
Last Name* John

Person Handling Request

Salutation Ms.
First Name* Alice
Last Name* Parish

I am using the following regex to get the first name: (?<=First Name\*). . So, I get Paul, what I want to do is having different regex to get the second matches only.

I tried using lookaround function :

(?=Person Handling Request) (?<=First Name\*). 

My question is, how would I go to get only the First Name from Person Handling Request?

CodePudding user response:

You can use

(?s)Person Handling Request.*?First Name\*\s*\K(?-s). 

See the regex demo. Details:

  • (?s) - make . match line break chars, too
  • Person Handling Request - a fixed string
  • .*? - any zero or more chars, as few as possible
  • First Name\* - a First Name* fixed string
  • \s* - zero or more whitespaces
  • \K - match reset operator that removes all text matched so far from the overall match memory buffer
  • (?-s) - disable DOTALL effect
  • . - match and return the rest of the line (one or more chars other than line break chars).
  • Related