Reputation: 529
I needed to ensure the string doesn't end with \'
so used negative look behind:
\\:'(.+)(?<!\\\\)'
However, it could end with \\'
. Basically, it could end with '
or '
preceded by the even number of backslashes \
.
It is implemented in Java.
Upvotes: 0
Views: 354
Reputation:
The only way
(?<!\\)(?:\\\\)*'$
Explained
(?<! \\ ) # Not an escape behind us, forces only even escapes ahead
(?: \\ \\ )* # Any amount of even escapes
' # Quote
$ # EOS
Notes - It is a fact that even escapes don't escape anything, so to enforce that only even escapes can be ahead, a negative look behind (?<!\\)
is used.
Upvotes: 1
Reputation: 159086
Write it as a regex to find what you don't want, then include that regex in a zero-width negative lookahead. Description below uses xxx
to indicate regex built up to that point.
You want to find \'
at the end: \\'$
You want to include even number of \
before that. (?:\\\\)*xxx
You don't want a \
before that: (?<!\\)xxx
Anything before that is allowed: .*xxx
Embed in a negative lookahead then match anything: ^(?!xxx).*$
Make sure .
matches line breaks (optional): (?s)xxx
All combined: (?s)^(?!.*(?<!\\)(?:\\\\)*\\'$).*$
As a Java literal: "(?s)^(?!.*(?<!\\\\)(?:\\\\\\\\)*\\\\'$).*$"
Demo on regex101.com shows that lines ending with odd number of \
before final '
is not selected, i.e. is invalid.
Upvotes: 0