简体   繁体   中英

RegEx match certain string until next occurence of this string

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

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/home and (?:(?!/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM