tags:

views:

96

answers:

3

Hi,

For an academic assignment, I need to make a regular expression to match a word with the following specifications:

  • Word length >= 1 and <= 8
  • Contains letters, digits, and underscore
  • First digit can only be a letter
  • Word is not A,X,S,T or PC,SW

I tried this regex but can't continue (My big problem is to make the word not equal to PC and SW)

([a-zA-Z&&[^AXST]])|([a-zA-Z][\w]{0,7})

But in the previous regex I didn't handle the that it is not PC and SW

Thanks,

+3  A: 

(?:^|\s)(?!PC)(?!SW)(?!A|S|T|X)(\w{1,8})

Explain this?

Highly suggest using an interactive regex tool for things like this. I use either http://regex.powertoy.org/ or http://gskinner.com/RegExr/ Then just play with them till you get it...

drewk
That "Explain" thing is pretty nice!
polygenelubricants
If prefix is allowed (e.g. `PCI` is okay), then the lookahead needs `$`, i.e. `(?!PC$)`.
polygenelubricants
+3  A: 

Use negative-lookahead (?!pattern)

^(?!(?:A|X|S|T|PC|SW)$)(?=[A-Za-z])(\w{1,8}$)

regular-expressions.info

polygenelubricants
A: 

rubular is a pretty good interactive regex tool as well.

Brendan Abel