encore2097
encore2097

Reputation: 503

Regex: match text square brackets and what is contained within square brackets then search and replace

I am trying to match (a) and replace (b) the following occurrences:

array[0] -> atoi(array[0])
array[1] -> atoi(array[1])
...
array[i+1] -> atoi(array[i+1])

and so on...

(a) I am unable to match anything with the following expression array\\[(.\*?)\\] , array\\[.\*?\\] , or array\\[*\\]

I am able to match single character occurrences between the brackets with array\\[.\\] and additionally also segments with multiples matches on a single line with array\\[.*\\]

(b) After a working match I figure s/"MATCHING REGEX"/atoi(array\[\1\])/g should work, however attempting that with array\\[.\\] resulted in atoi(array[])

Upvotes: 2

Views: 10303

Answers (3)

Benoit
Benoit

Reputation: 79185

You can use:

:s/array\[.\{-}\]/atoi(&)

Upvotes: 1

shinkou
shinkou

Reputation: 5154

How about this?

:s/\<array\[[^\]]\+\]/atoi(\0)/

Upvotes: 4

Murray McDonald
Murray McDonald

Reputation: 631

Well you don't really say what RegEx engine you are using but if I had to guess it may be that this particular engine doesn't like the "non-greedy" qualifier. So let's try the regex eleminating the non-greedy qualifier and using a character class of "not closing square bracket" in place of the non-greedy ".*?". Try this instead:

array[([^]]*)]

Upvotes: 0

Related Questions