tags:

views:

60

answers:

1

Hello again Stackoverflow people!

Assume I have these words: smartphones, smartphone

I want to match the substring "phone" from within them. However, in both case, I want only "phone" to be returned, not "phones" in the first case. In addition to this, I want matches only if the word "phone" is a suffix only, such that:

fonephonetics (just an example) is not matched.

I assumed that the regex

(phone([?=s])?)\b

would give me what I need, but it is currently matching "phones" and "phone", but not the "fonephonetics" one. I don't need "phones". I want "phone" for both cases.

Any ideas about what is wrong, and what I can do?

Thank you in advance!

+5  A: 

To match phone followed by either s\b or \b:

phone(?=s?\b)

The lookahead is a zero-width match so the s won't be returned as part of the match.

Mark Byers
Your solution works. What if I have to use alternation to match a series of other words as suffixes?(phone(?=s?\b)|test|test2)\b won't work then.Any solutions?
Inf.S
@Inf: Isolate the alternation in its own set of parentheses: `(phone|test|test2)(?=s?\b)`
Alan Moore
Many thanks! That is what I wanted! :)Could you please tell me why, when putting the (?=s?\b) in its own parenthesis, it works? Or point me to some link where I can get that info?
Inf.S
Inf.S: It's a zero-width lookahead meaning that it matches 0 characters. You can read more about it here: http://www.regular-expressions.info/lookaround.html
Mark Byers