PHP/PostGres 正则表达式仅在字符串中的单词有 2 个或更多大写且仅包含字母时匹配

发布于 2024-09-28 12:06:28 字数 538 浏览 1 评论 0原文

这看起来是一个简单的表达,但我所有的修修补补都在某个地方失败了。我正在从 PostgreSQL 数据库中提取数据。我宁愿在 PostgreSQL 中进行过滤,但如果我需要在 PHP 中进行过滤,也可以。该列将有一个字符串,我需要搜索任何单词(由空格定义)具有两个或多个大写字母的匹配项。如果我需要分解每个字符串并单独解析“单词”,我也可以这样做,但似乎正则表达式会是更好的方法。

以下是一些示例:

“The Quick Brown”//不匹配

“QuicK Brown”//比赛(注意 QuicK 有 2 个大写字母)

“快速棕色”//比赛

"The QUICK1 Brown" //不匹配(QUICK 全部大写,但也有非字母字符)

"The QUICK BROWN1" //匹配,因为 QUICK 很受欢迎,尽管 BROWN1 不是。

“快速棕色”//不匹配

"The QUICK-BROWN" //不匹配(因为 - 不是字母,单词仅由空格定义)

“快速棕色”//不匹配

This seems like a simple expression, but all of my tinkering has failed in one place or another. I am pulling data out of a PostgreSQL database. I would rather filter in PostgreSQL, but if I need to do it in PHP that is fine too. The column will have a string, and I need to search for matches where any word (defined by spaces) has two or more capital letters. If I need to just explode each string and parse the "words" individually, I can do that too, but it seems like a regexp would be a much better way.

Here are some examples :

"The Quick Brown" //No Match

"The QuicK Brown" //MATCH (note QuicK has 2 caps in it)

"THE QUICK BROWN" //Match

"The QUICK1 Brown" //No Match (QUICK is all caps, but it has a non alpha character too)

"The QUICK BROWN1" //Match because QUICK is a hit even though BROWN1 is not.

"tHe qUick bRown" //No Match

"The QUICK-BROWN" //No Match (because - is non alpha, and words are only defined by space)

"the quick brown" //No Match

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

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

发布评论

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

评论(1

北座城市 2024-10-05 12:06:28
(^| )[A-Za-z]*[A-Z][A-Za-z]*[A-Z][A-Za-z]*( |$)

通过 http://regexr.com?2sdi4 检查

(^| )[A-Za-z]*[A-Z][A-Za-z]*[A-Z][A-Za-z]*( |$)

Checked via http://regexr.com?2sdi4

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