This is a pretty simple regex question, I think. In Vim, if I want do a search that matches planA or planB, I know that I can do this:

/plan[AB]

This works because the regex allows for either A or B as its set of characters.

But how can I specify one of two complete strings? For example, planetAwesome or planetTerrible?

This will match both of these, along with planetAnythingHereAsLongAsItsJustLetters:

planet\([a-zA-Z]*\)

But how can I match only strings that match planetAwesome or planetTerrible exactly?

link|improve this question

62% accept rate
Actually, the brackets create a character class. plan[ABC] matches planA, planB, and planC equally well. – molecules Feb 23 '11 at 22:47
@molsecules - right, thanks. I updated the question. – Nathan Long Feb 23 '11 at 22:53
feedback

2 Answers

up vote 5 down vote accepted
/planet\(Awesome\|Terrible\)

To see the relevant documentation, issue :help /, and scroll down to the section “The definition of a pattern”.

link|improve this answer
Ah! I wasn't escaping the parentheses when I tried this. Interesting - I thought I only had to escape literal characters. – Nathan Long Feb 23 '11 at 23:39
@Nathan: Various regex engines have different rules about which characters must be escaped to make them special vs. must be escaped to make them literal (e.g. POSIX BRE vs. ERE). You can tweak the escaping requirements in Vim on a per-pattern basis by including one of the special escapes: \m, \M, \v, or \V. There is also the 'magic' option, but it is usually best to leave it alone, since it has global effect (unless overridden by one of the above flags). – Chris Johnsen Feb 24 '11 at 5:51
feedback

To add to Gilles' answer, you might want to add a few things in there:

/\<planet\(Awesome\|Terrible\)\>

\< marks the beginning of a 'word' (essentially alphanumerics)
\> marks the end of a 'word' (essentially alphanumerics)

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.