正则表达式检查确切的字符串是否存在

发布于 2024-09-01 16:58:43 字数 620 浏览 6 评论 0原文

我正在寻找一种方法来使用正则表达式或建议的任何更好的方法来检查另一个字符串中是否存在精确的字符串匹配。据我所知,您告诉正则表达式在字符串的开头或结尾匹配空格或任何其他非单词字符。但是,我不知道具体如何设置。

搜索字符串:t

字符串 1:Hello World,很高兴见到你! t
字符串 2:Hello World,很高兴见到你!
字符串 3:T Hello World,很高兴见到你!

我想使用搜索字符串并将其与字符串 1、字符串 2 和字符串 3 进行比较,并且仅从字符串 1 和字符串中获得正匹配3 但不是来自字符串 2。

要求:
搜索字符串可以位于主题中的任何字符位置。
它之前或之后可能有也可能没有空白字符。
如果它是另一个字符串的一部分,我不希望它匹配;例如单词的一部分。

为了这个问题: 我想我会使用以下模式来做到这一点: /\bt\b/gi

/\b{$search_string}\b/gi

这看起来正确吗?可以做得更好吗?这种模式在什么情况下不起作用?
附加信息:这将在 PHP 5 中使用

I am looking for a way to check if an exact string match exists in another string using Regex or any better method suggested. I understand that you tell regex to match a space or any other non-word character at the beginning or end of a string. However, I don't know exactly how to set it up.

Search String: t

String 1: Hello World, Nice to see you! t
String 2: Hello World, Nice to see you!
String 3: T Hello World, Nice to see you!

I would like to use the search string and compare it to String 1, String 2 and String 3 and only get a positive match from String 1 and String 3 but not from String 2.

Requirements:
Search String may be at any character position in the Subject.
There may or may not be a white-space character before or after it.
I do not want it to match if it is part of another string; such as part of a word.

For the sake of this question:
I think I would do this using this pattern: /\bt\b/gi

/\b{$search_string}\b/gi

Does this look right? Can it be made better? Any situations where this pattern wouldn't work?
Additional info: this will be used in PHP 5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

闻呓 2024-09-08 16:58:43

您对 /\bt\b/gi 的建议将会起作用,并且可能是可行的方法。您已正确使用 \b 作为字边界。您正在使用全局和不区分大小写的修饰符,它将找到两种情况下的所有匹配项。简单、直接、干净。只要看看你已经想出的东西就可以了。

Your suggestion of /\bt\b/gi will work and is probably the way to go. You've correctly used \b for word boundaries. You're using the global and case-insensitive modifiers which will find all matches in both cases. Simple, straight forward, clean. Look no further than what you've already come up with.

叶落知秋 2024-09-08 16:58:43

对我来说看起来不错。您可能想检查 \b 断言 确保它正是您所需要的。

如果没有更详细的描述,无法真正说出此模式“不起作用”的任何情况,但 \b 对于您的测试用例来说可以正常工作。

Looks fine to me. You might want to check the exact meaning of the \b assertion to make sure it's exactly what you need.

Can't really name any situation where this pattern "wouldn't work" without a more elaborate description, but \b would work fine for your testcases.

待"谢繁草 2024-09-08 16:58:43

根据老话,给一个人一个正则表达式,他会高兴一天,教他写正则表达式,他会高兴一辈子(或类似的东西)尝试一下“regulator"

它提供了一个 GUI 和一些非常好的例子来满足 reg exp 的需要。

According to the old saying give a man a reg expression and he is happy for a day, teach him to write regular expression and he is happy for a lifetime (or something to that effect) try out the "regulator"

It provides a GUI and some pretty good examples for reg exp needs.

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