How do I write a regex that accepts an expression that contains any number of any characters except for '[
', unless '[
' is preceded by '\
' ?
Example:
this is text \\[ this also [$ this isn't any more
From the above text, "this is text \\[ this also
" should be accepted, and the rest shouldn't. I wrote something like:
[.[^\\\\[]]*
to exclude the '[
' but have no idea how to allow it to contain '\\[
' and the rest of the text also.
This will match all characters that are either not equal to [
or equal to a [
preceded by \
:
([^\[]|(?<=\\)\[)+
If you want a simple pass/fail for an entire string, just add the start/end-line characters to the regex:
^([^\[]|(?<=\\)\[)+$
([^\[]|\\\[)*
This accepts a sequence of ((anything except [
) or (\[
)).
In general, if you want to accept a string where certain characters need escaping, e.g. abcd
, the regex that matches it is:
([^abcd]|\\[abcd])*
Edit:
This regex can be used with Matcher.find
to iterate over all the sections that are within/outside []
:
\[(?:[^\[]|\\\[)*\]|(?:\\\[|[^\[])+
(Double all the backslashes when putting it in a Java string; I'm leaving them out for legibility.)
This will split the string abc[ def \[ asd \] ]\[ dasd[ \] ]
into abc
, [ def \[ asd \] ]
, \[ dasd
, and [ \] ]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With