Regex positive lookbehind after digits

Viewed 112

Trying to get the all characters until new line after one/more digits with positive lookbehind from below text with this

(?<=below customer.\s.*\n.* )(.*)
I order standardinstalation to below customer.
Paul Rilley
Abbeyroad 55

It works (gives 55) if the roadname does not have a space. Not working with (High Tory road). Also there could be letters after the digits (55b) that I should get. I need to look behind the words (below customer) since the first line is the only part that is always the same.

2 Answers

You can use

(?m)(?<=below customer\.\r?\n(?:.+\n)*?.+ )(\d+[A-Za-z]*)\r?$

See the .NET regex demo.

Details:

  • (?m) - multiline mode to make $ match end of any line is on
  • (?<=below customer\.\r?\n(?:.+\n)*?.+ ) - the lookbehind to match below customer., then a line ending sequence, then zero or more lines with a line ending sequence, as few as possible, and then zero or more chars other than newline till the last space followed with
  • (\d+[A-Za-z]*) - Group 1: one or more digits and then zero or more letters
  • \r?$ - an optional CR char and the end of line.

It will also match 55b.

In most regex flavors, a lookbehind must be fixed width. In .NET, variable width is supported.

You can use both in PCRE and .NET:

/(?<=below customer\.)\r?\n.*\r?\n.* (\w+)$/gm

Demo for PCRE

Demo for .NET

Related