You need a negative lookbehind here:
(?<!\\)\*[^*]+(?<!\\)\*
In a Java string, this gives (backslash galore):
"(?<!\\\\)\\*[^*]+(?<!\\\\)\\*"
Note: the star (*
) has no special meaning within a character class, therefore there is no need to escape it
Note 2: (?<!...)
is a negative lookbehind; it is an anchor, which means it finds a position but consumes no text. Literally, it can be translated as: "find a position where there is no preceding text matching regex ...
". Other anchors are:
^
: find a position where there is no available input before (ie, can only match at the beginning of the input);
$
: find a position where there is no available input after (ie, can only match at the end of the input);
(?=...)
: find a position where the following text matches regex ...
(this is called a positive lookahead);
(?!...)
: find a position where the following text does not match regex ...
(this is called a negative lookahead);
(?<=...)
: find a position where the preceding text matches regex ...
(this is a positive lookbehind);
\<
: find a position where the preceding input is either nothing or a character which is not a word character, and the following character is a word character (implementation dependent);
\>
: find a position where the following input is either nothing or a character which is not a word character, and the preceding character is a word character (implementation dependent);
\b
: either \<
or \>
.
Note 3: Javascript regexes do not support lookbehinds; neither do they support \<
or \>
. More information here.
Note 4: with some regex engines, it is possible to alter the meaning of ^
and $
to match positions at the beginning and end of each line instead; in Java, that is Pattern.MULTILINE
; in Perl-like regex engines, that is /m
.