1

I need to match a string (let's name it "/export/home") and everything that follows (including optional new lines) until the next occurence of this string.

I tried /(/export/home.\n.)(.)/ but it won't match the correct set from string to string.

Example:

/export/home/bla "123" "bla" test 1
/export/home/bla "123" "bla" test 2
/export/home/bla "123" "bla" test 3
test4
test5
/export/home/bla "123" "bla" test 6
/export/home/bla "123" "bla" test 7
/export/home/bla "123" "bla" test 8
test9
/export/home/bla "123" "bla" test 1

Everything and including from /export/home until the next /export/home should be matched. Any help is appreciated, thanks in advance

Royce
  • 555
  • 1
  • 8
  • 14

1 Answers1

1

You may use a tempered greedy token:

(?s)/export/home(?:(?!/export/home\b).)*
                ^^^^^^^^^^^^^^^^^^^^^^^^

See the regex demo

The (?s) will make . match any char including line break chars, /export/home will match /export/homeand (?:(?!/export/home).)* will match any char (.), zero or more occurrences (*) that does not start a /export/home literal char sequence.

If you unroll the pattern, it will look like

/export/home/[^/]*(?:/(?!export/home/)[^/]*)*

See this demo

Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563