0
 input = ' 12Z taj 20501 jfdjda OCNL jtjajd ptpa 23Z jfdakdkf tjajdfk OCNL fdkadja 02Z fdjafsdk fkdsafk OCNL fdkafk dksakj = '

using regexp

regexp(input,'\s\d{2,4}Z\s.*(OCNL)','match')

I'm trying to get the output

[1,1] = 12Z taj 20501 jfdjda OCNL jtjajd ptpa

[1,2] = 23Z jfdakdkf tjajdfk OCNL fdkadja

[1,3] = 02Z fdjafsdk fkdsafk OCNL fdkafk dksakj

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
John
  • 157
  • 1
  • 10

1 Answers1

1

You may use

(?<!\S)\d{2,4}Z\s+.*?\S(?=\s\d{2,4}Z\s|\s*=\s*$)

See the regex demo.

Details

  • (?<!\S) - there must be a whitespace or start of string immediately to the left of the current location
  • \d{2,4} - 2, 3 or 4 digits
  • Z - a Z letter
  • \s+ - 1+ whitespaces
  • .*?\S - any zero or more chars as few as possible and then a non-whitespace
  • (?=\s\d{2,4}Z\s|\s*=\s*$) - there must be either of the two patterns immediately to the right of the current location:
    • \s\d{2,4}Z\s - a whitespace, 2, 3 or 4 digits, Z and a whitespace
    • | - or
    • \s*=\s*$ - a = enclosed with 0+ whitespace chars at the end of the string.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563