tags:

views:

35

answers:

4

Hello,

I am trying to change one of the legacy code that declares everything in arrays to a collection. I'd like to do a simple search-and-replace using regular expressions and I was wondering if anyone could help me out.

For example, if arrays are declared this way:

array[0] = "1"; array[1] = "2"; array[2] = "3"; array[3] = "4"; array[4] = "5"; array[5] = "6"; array[6] = "7"; array[7] = "8";

I would like to grep array[{number}] and replace it with something like list.add(

The regular expression that I am trying to use is array[[0-9]+] but I am getting a syntax error. If anyone could give a helping hand, I would really appreciate it. Thanks a lot.

+3  A: 

Try escaping the outer square brackets:

array\[[0-9]+\]
Robusto
Thanks a lot. I didn't know that I had to escape closing brace as well.
thomas1234
Square brackets are used to specify ranges and multiple characters. If you are searching for square brackets in text, they must be escaped. Also, if this solves the problem for you, consider accepting the answer by clicking the checkmark. A good acceptance rate will identify you as a good citizen here and encourage people to answer your questions.
Robusto
Apparently I have to wait ~15 minutes after posting the question to accept an answer.
thomas1234
A: 

I believe you will have to escape your square brackets, as they would normally signify an option group. Use the \ to escape each of your outer square brackets

Andrew Barber
A: 

Google Regex workbench It is an interactive free tool to let you try out regular expressions on your samples to see what works. It also explains how the regex is being interpreted.

Roadie57
+2  A: 

regexpal is a really useful tool for testing regular expressions, you should try it.

ncardeli