Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
Actually, the brackets create a character class. plan[ABC] matches planA, planB, and planC equally well. –  Christopher Bottoms Feb 23 '11 at 22:47
    
@molsecules - right, thanks. I updated the question. –  Nathan Long Feb 23 '11 at 22:53

2 Answers 2

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

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

share|improve this answer
1  
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

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)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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