Romonov
Romonov

Reputation: 8605

gvim search match multiple characters using regex

I am trying to search in gvim for the following pattern:

arrayA[*].entryx

hoping it would match the following:

arrayA[size].entryx  
arrayA[i].entryx
arrayA[index].entryx

but it prints message saying Pattern not found even though the above lines are present in the file.

arrayA[.].entryx  

only matches arrayA[i].entryx i.e. with only one character between [] braces.

What should I do to match multiple characters between [] braces?

Upvotes: 2

Views: 2097

Answers (3)

nik
nik

Reputation: 13450

Here is the PCRE expression detail

/arrayA\[[^]]*]\.entryx/
         ^^^^^            # 0 or more characters before a ']'
       ^^      ^^         # Escaped '[' & '.'
              ^           # Closing ']' -- does not need to be escaped
 ^^^^^^          ^^^^^^   # Literal parts

If you want to look for arrayA[X].entryx where, there is at least on character in the [],
You need to replace \[[^]]* with \[[^]]\+

ps: Note my edit -- I've changed the \* to just * -- you don't escape that either.
But, you need to escape the + :-)


Update on your comment:
While my comment answers your question on escaping ] broadly,
for more detail look at Perl Character Class details.
Specifically, the Special Characters Inside a Bracketed Character Class section.
Rules of what needs to be escaped change after a [character starts a Character Class (CCL).

Upvotes: 2

Magnun Leno
Magnun Leno

Reputation: 2738

Always remember that in VIM you need to scape some special characters, such as [, ], {, } and .. As said before the *repeats the previous character, with this you can simply use the /arrayA\[.*\]\.entryx, but the * is greedy character, it may match some strange things, add the following line to your file and you'll understand: arrayA[size].entryx = arrayB[].entryx

A "safer" Regular Expression would be:

/arrayA\[.\{-\}\]\.entryx

The .\{-\} matches any character in a non-greedy way, witch is safer for some cases.

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 753555

The * repeats the previous character; and [ starts a character class. So, you need something more like:

/arrayA\[[^]]*]\.entryx/

That looks for a literal [, a series of zero or more characters other than ], a literal ], a literal . and the entryx.

Upvotes: 1

Related Questions