Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to exclude [ unless preceded by \

Tags:

java

regex

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.

like image 982
Carkak Vognema Avatar asked Mar 22 '13 15:03

Carkak Vognema


2 Answers

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:

^([^\[]|(?<=\\)\[)+$
like image 179
woemler Avatar answered Nov 08 '22 13:11

woemler


([^\[]|\\\[)*

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 [ \] ].

like image 4
ebsddd Avatar answered Nov 08 '22 13:11

ebsddd