开发者

Regex to match a word suffix only when it appears as part of larger word?

The following regexp matches t开发者_如何学Gohe trailing 's' on a word or word fragment:

/s\b/i

Is it possible to only match the trailing s if it is a part of a larger word?

That is, for example, the suffix s in the string "words" is matched, but if someone entered a string "s" by itself, there would be no match.

Thanks for your advice


Like this where \w patches any 'word' character followed by s followed by word boundary

/\ws\b/i


The standard answer is

/\ws\b/i

as was given before, but there are caveats. Make sure that Perl's definition of \w is suitable for your use. Perl thinks that '_' is a word character, so this will match '2_s' as a suffixed word. You can use \p{IsAlpha} instead if you want just alphabetic characters.

You still have the issue that the above will think that '214bs' is the "word" 214b with an 's' on the end. Instead you might want something like this:

/\b\p{IsAlpha}+s\b/

It all depends on what your input looks like.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜