Regex first word after match and last word in specific line

Viewed 192

I am stuck with the following expression.

RECOVER FROM:
Company 1
Bla: Bla
Bla: Bla

DELIVER TO:
Sypris Solutions, Inc.
Street 123
City ZIP US
Bla: Bla
Bla: Bla
Bla: Bla

I would like to filter the company information which starts after "DELIVER TO:"

With \bDELIVER TO:\s+\K\S+, I get the first word in the first line after "DELIVER TO:" (which is fine because we only need to know the first word of the Company name to get the clue).

The goal is to have the first word in the line after "DELIVER TO:" to return as Group 1. The last word in the third line (in this case the country code "US") should return as Group 2.

https://regex101.com/r/hU6lfr/1

Any help is much appreciated.

1 Answers

You can use

\bDELIVER TO:\R\h*(\S+).*(?:\R.*){2}.*\h(\S+)

See the regex demo

Details:

  • \b - a word boundary
  • DELIVER TO: - a substring
  • \R - a line break char sequence
  • \h* - zero or more horizontal whitespaces
  • (\S+) - Group 1: one or more non-whitespaces
  • .* - any zero or more chars other than line break chars, as many as possible (the rest of the line)
  • (?:\R.*){2} - two lines
  • .* - any zero or more chars other than line break chars, as many as possible
  • \h - a horizontal whitespace
  • (\S+) - Group 2: one or more non-whitespaces.
Related