正则表达式仅在单词后缀作为较大单词的一部分出现时才匹配该单词后缀?
以下正则表达式匹配单词或单词片段上的尾随 's':
/s\b/i
是否可以仅匹配尾随 s 如果它是较大单词的一部分?
也就是说,例如字符串“words”中的后缀s是匹配的,但如果有人自己输入字符串“s”,则不会匹配。
谢谢你的建议
The following regexp matches the 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样, \w 修补任何“单词”字符,后跟 s 后跟单词边界
Like this where \w patches any 'word' character followed by s followed by word boundary
标准答案
如前所述,但有一些注意事项。确保 Perl 的 \w 定义适合您的使用。 Perl 认为 '_' 是一个单词字符,因此这将匹配 '2_s' 作为后缀单词。如果您只需要字母字符,可以使用 \p{IsAlpha} 代替。
你仍然有一个问题,上面的代码会认为'214bs'是“单词”214b,末尾有一个's'。相反,您可能想要这样的东西:
这完全取决于您的输入是什么样的。
The standard answer is
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:
It all depends on what your input looks like.