tags:

views:

66

answers:

2

Hi,

I am trying to get a regular expression that will match on any of the following strings:

Sales Director:
Managing Director:
Sales Manager:
General Manager - Power:
Finance Director:
Procurement Manager:
Director:
Director:
Managing Director:
Finance Director:
Sales Director:
Managing Director:
Sales Manager:
Finance Director:
Procurement Manager:

But not on:

print directories and guides
defence Industry Directory

so far I have:

/(manager|director)/i

But this is obviously going to match on directories, directory etc.

I know the $ sign signifies the end of a word but I cannot get the right syntax.

Should I be using word boundaries like \b?

I also DO NOT want to pull back whole sentences with manager or director in the sentence. I think I want a maximum of 2 words where manager or directory is the first or second word. Is this possible?

Can anyone point me in the right direction?

Cheers

Paul

+2  A: 

Should I be using word boundaries like \b?

Yes, you need word boundaries here:

/\b(manager|director)\b/i

I think I want a maximum of 2 words where manager or director is the first or second word. Is this possible?

Yes, you can do this with a single regular expression (rubular):

/^(?:\w+ +)?(manager|director)\b/i

This searches for either:

  • the word manager or director at the beginning of the line or
  • one word, one or more spaces, then the word manager or director
Mark Byers
That is really cool, could you explain the ?: syntax?
dagda1
@dagda: It makes the group non-capturing. You can remove the `?:` and see how it affects the results (see the part where it says "match captures" underneath the match results).
Mark Byers
A: 

Different "flavours" of regex have different capabilities, so you should tag your question with the technology in which you are working.

If your regex engine supports negative lookahead (a "zero-width assertion"), you can use director(?![yi]) to exclude instances of director that are followed by a y or i.

Really, a word boundary is probably just fine.

Jay