正则表达式:正向前瞻和字边框问题
Stackoverflow 的朋友们大家好!
假设我有这些词:智能手机,智能手机
我想匹配其中的子字符串“phone”。但是,在这两种情况下,我只想返回“电话”,而不是第一种情况中的“电话”。除此之外,我只希望当“phone”这个词只是后缀时才匹配,这样:
fonephonetics (只是一个例子)不匹配。
我认为正则表达式
(phone([?=s])?)\b
会给我我需要的东西,但它目前匹配“phones”和“phone”,而不是“fonephonetics”。我不需要“电话”。这两种情况我都想要“电话”。
关于出了什么问题以及我能做什么有什么想法吗?
先感谢您!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要匹配
phone
后跟s\b
或\b
:先行查找是零宽度匹配,因此
s 不会作为匹配的一部分返回。
To match
phone
followed by eithers\b
or\b
:The lookahead is a zero-width match so the
s
won't be returned as part of the match.