tags:

views:

46

answers:

2

I am highlighting words in a body of text.

If I regex "Port" the word "Portuguese" is highlighted too, but I need "Ports" to be highlighted.

Any ideas? I hate regex.

Thanks

+3  A: 

Try something like this:

\bports?\b

The ? means that the s character is optional. The \b at either end matches word-boundaries.

More generally, you could do something like this to allow words ending in s, es or ies:

\bwhatever(?:s|es|ies)?\b

This is very crude and you're likely to get false positives and negatives. If you want something more sophisticated then I suppose you'd need to look at a proper full-text search engine.

LukeH
This is what I was going to suggest.
jimplode
its just for text matching in an iphone application, I'm sure this will suffice. There is a finite amount of text too, so any issues I encounter will be easy to change. Thank you very much for this. :)
Thomas Clayson
+1  A: 

The most basic answer would be this:

\bPort(s?)\b

'\b' marks beginning and ending of the word. This only matches 'Port' and 'Ports'. If you need case insensitive matching, then use something like /i modifier in Perl:

m/\bport(s?)\b/i

Or, if you want to match only 'port', 'Port', 'ports' and 'Ports', try

\b(P|p)ort(s?)\b

jsolo13