eatonphil
eatonphil

Reputation: 13712

Regex excluding square brackets

I am new to regex. I have this regex:

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

Basically it should take this:

[[a][b][[c]]]

And be able to replace with:

[dd[d]]

abc, d are unrelated. Needless to say the regex bit isn't working. it replaces the entire string with "d" in this case.

Any explanation or aid would be great!

EDIT:

I tried another regex,

\[([^\]]{0})\]

This one worked for the case where brackets contain no inner brackets and nothing else inside. But it doesn't work for the described case.

Upvotes: 5

Views: 15604

Answers (3)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89629

Try this:

System.out.println("[[a][b][[c]]]".replaceAll("\\[[^]\\[]]", "d"));

if a,b,c are in real world more than one character, use this:

System.out.println("[[a][b][[c]]]".replaceAll("\\[[^]\\[]++]", "d"));

The idea is to use a character class that contains all characters but [ and ]. The class is: [^]\\[] and other square brackets in the pattern are literals.

Note that a literal closing square bracket don't need to be escaped at the first position in a character class and outside a character class.

Upvotes: 1

Pshemo
Pshemo

Reputation: 124275

You need to know that . dot is special character which represents "any character beside new line mark" and * is greedy so it will try to find maximal match.

In your regex \[(.*[^(\]|\[)].*)\] first .* will represent maximal set of characters between [ and [^(\]|\[)].*)\]] and this part can be understood as non [ or ] character, optional other characters .* and finally ]. So this regex will match your entire input.


To get rid of that problem remove both .* from your regex. Also you don't need to use | or ( ) inside [^...].

System.out.println("[[a][b][[c]]]".replaceAll("\\[[^\\]\\[]\\]", "d"));

Output: [dd[d]]

Upvotes: 7

Patashu
Patashu

Reputation: 21793

\[(\[a\])(\[b\])\[(\[c\])\]\]

If you need to double backslashes in the current context (such as you are placing it in a "" style string):

\\[(\\[a\\])(\\[b\\])\\[(\\[c\\])\\]\\]

An example replacement for a, b and c is [^\]]*, or if you need to escape backslashes [^\\]]*.

Now you can replace capture one, capture two and capture three each with d.


If the string you are replacing in is not exactly of that format, then you want to do a global replacement with

(\[a\])

replacing a,

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

doubling backslashes,

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

Upvotes: 1

Related Questions