正则表达式:正向前瞻和字边框问题

发布于 2024-08-25 15:39:14 字数 377 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落成白 2024-09-01 15:39:14

要匹配 phone 后跟 s\b\b

phone(?=s?\b)

先行查找是零宽度匹配,因此 s 不会作为匹配的一部分返回。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文