开发者

Regular expression for staff member

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 sent开发者_如何学Pythonences 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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜