tags:

views:

84

answers:

3

How do I write a regular expression that will match all words starting with I except the word Integer?

Example:

  t1: Integer;
  t2: Interface;
  t3: IXml;

The result should be Interface and IXml.

+3  A: 

This should do:

I(?!nteger\b)\w+
SilentGhost
… although Pedro, unfortunately, did not specify which flavor of regex he has. Not all regex libraries support negative lookahead.
Christopher Creutzig
You should remove the \w because it doesn't match de semicolumn ; given in examples.
M42
@M42: the semicolon was not meant to be matched.
SilentGhost
@Chris: http://www.regular-expressions.info/refflavors.html any modern language supports negative look-ahead. Besides, it's not the reason to devalue my answer, and rather a reason to ask OP for clarifications.
SilentGhost
@SilentGhost: Sorry, you're right, I misunderstood
M42
@SilentGhost: I didn't downvote your answer, I had in fact upvoted it.
Christopher Creutzig
@Christopher: I never said you did. Thanks for the upvote, though.
SilentGhost
A: 

How about this :

I(?!nteger).*
DrunkenBeard
+1  A: 

If you can’t use a look-ahead assertion as SilentGhost suggested, you can express the same using basic regular expression syntax:

I(\b|[A-Za-mo-z][A-Za-z]*|n(\b|[A-Za-su-z][A-Za-z]*|t(\b|[A-Za-df-z][A-Za-z]*|e(\b|[A-Za-fh-z][A-Za-z]*|g(\b|[A-Za-df-z][A-Za-z]*|e(\b|[A-Za-qs-z][A-Za-z]*|r[A-Za-z]+))))))
Gumbo