Carkak Vognema
Carkak Vognema

Reputation: 93

Regex to exclude [ unless preceded by \

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.

Upvotes: 9

Views: 1292

Answers (2)

ebsddd
ebsddd

Reputation: 1026

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

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

Upvotes: 4

woemler
woemler

Reputation: 7169

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:

^([^\[]|(?<=\\)\[)+$

Upvotes: 5

Related Questions