Reputation: 131
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
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+ timesstart.*
Match start followed by any character 0+ timesUpvotes: 4