Lyu JH
Lyu JH

Reputation: 131

Start Before End

This is a quiz exercise

Only match lines with the text start, unless the text end is before that (end may or may not be in the string). Match: ssstarttt line And don't match line_end start

I tried the code

(?:.*?\s?start(?<!$).*\b|(?<!.)start|^\w+start)

Results returns

Test 23/25: It shouldn't match end start end start end start end start.

Regex demo

Should be matched

ssstarttt line

estart

start

aa start end start

Should not be matched

line_end start

end start end start end start end start

Upvotes: 2

Views: 284

Answers (1)

The fourth bird
The fourth bird

Reputation: 163207

If the text start should be there but the text end should not be before start, one option is to use a negative lookahead to assert what is on the right is not end

^(?:(?!end).)*start.*

Explanation

  • ^ Start of the string
  • (?: Non capturing group
    • (?!end). Negative lookahead to assert what is on the right is not end and match any character
  • )* Close non capturing group and repeat 0+ times
  • start.* Match start followed by any character 0+ times

regex101 demo

Upvotes: 4

Related Questions