php - 尝试使用正则表达式查找字母组

标签 php regex search find

我需要找到 1 个或多个用括号括起来的已定义字符组。如果存在多个组,将用连字符分隔。

例子:

(us)
(jp)
(jp-us)
(jp-us-eu)

如果字符串只包含一个组,我已经想出了如何找到组:

/\(us\)|\(jp\)/

但是,当我找到多个由连字符分隔并且没有特定顺序的时,我感到很困惑:(us-jp) OR (jp-us)

感谢任何帮助。

谢谢, 西蒙

最佳答案

\((\b(?:en|jp|us|eu)-?\b)+\)

解释:

\(                     // opening paren
(                      // match group one
  \b                   // word boundary
  (?:en|jp|us|eu)      // your defined strings
  -?                   // a hyphen, optional
  \b                   // another word boundary
)+                     // repeat
\)                     // closing paren

matches:

(us)
(jp) 
(jp-us)
(jp-us-eu)

不匹配:

(jp-us-eu-)
(-jp-us-eu)
(-jp-us-eu-)

关于php - 尝试使用正则表达式查找字母组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1481798/

相关文章:

正则表达式在特定字符处停止

algorithm - 为最近邻搜索 (NNS) 修改此算法以执行近似 NNS

php - 在 MySQL 和 PHP 中返回找到值的行的 id

php - 使用两个条件自定义排序数组数组

正则表达式替换 : only replace the first occurance from a "or" condition

PHP 未定义的 mysqli_result

javascript - 如何检测后面没有另一个换行符的换行符?

jquery - 我需要在 Framework7 中实现自定义搜索

phpseclib 与 libssh2

php - 如何对表格进行排序和搜索