Reputation: 3
With regex, I would like to use a negative lookbehind to match a pattern only if it is not near the beginning of the line. This is for matching VHDL comments that follow other text, but not comments that are near the beginning of a line (i.e. with no other text before them except whitespace).
For example, if my test string is:
-- BOL
test; -- C1
test => -- C1
-- BOL indent
I would want a regex string:
(?<!^\s*)--
To only match "--" in front of the two "C1"s, and ignore the "--" in front of "BOL" and "BOL indent". But this would need variable length lookbehind, which is not supported in this regex tool. The problem stems from not knowing ahead of time how many spaces the indents will be.
Is there a better way to do this?
Upvotes: 0
Views: 104
Reputation: 110665
I assume you wish to match '--'
and everything that follows in the line or string, provided '--'
is preceded by a non-whitespace character.
It was noted in a comment on the question that the regex engine supports \K
. \K
causes the regex engine to discard everything in the current match and reset the starting point of the match to the current location in the string.
\S.*\K--.*
I tested this with the PCRE (PHP) regex engine, which supports \K
. Demo
If you don't want to include '--'
in the match use \S.*--\K.*
or \S.*--\s*\K.*
.
Upvotes: 0
Reputation: 163207
One option is to match 0+ whitespace chars from the start of the string, then match at least non whitespace char that is not a -
. Then capture --
further in the string in a group
^[^\S\r\n]*[^\s-].*(--)
Upvotes: 1