RegEX以匹配字符串

时间:2018-03-16 11:43:26

标签: php regex

我试图编写RegEX来匹配下一个字符串中的所有事件类型(应该以空白字符或括号结束。

(STUNNEL-SECURITY AND NOT (
    eventtype=sec-stunnel-client-* 
 OR eventtype=sec-stunnel-crl-* 
 OR eventtype=sec-stunnel-loaded-* 
 OR eventtype=sec-stunnel-loading-* 
 OR eventtype=sec-stunnel-no-* 
 OR eventtype=sec-stunnel-openssl-* 
 OR eventtype=sec-stunnel-sconnect-* 
 OR eventtype=sec-stunnel-server-* 
 OR eventtype=sec-stunnel-service-* 
 OR eventtype=sec-stunnel-session-* 
 OR eventtype=sec-stunnel-socket-* 
 OR eventtype=sec-stunnel-ssl-* 
 OR eventtype=sec-stunnel-stats-* 
 OR eventtype=sec-stunnel-threading-* 
 OR eventtype=sec-stunnel-timeout-* 
 OR eventtype=sec-stunnel-various-* 
) )

我需要获得所有比赛的数组,例如。 array("sec-stunnel-client-*", "sec-stunnel-crl-*", "sec-stunnel-loaded-*"...)

应如何做到这一点?

2 个答案:

答案 0 :(得分:2)

试试这个正则表达式:

(?<=eventtype=)\S*

查找前面带有eventtype=的非空白字符。 在线试用here

答案 1 :(得分:1)

您可以使用\K代替积极前瞻(?<=)更少的步骤。

正则表达式eventtype=\K\S+

详细说明:

  • \K重置报告的匹配的起点
  • \S+匹配一次和无限次之间的任何非空格字符

PHP代码

preg_match_all("~eventtype=\K\S+~", $str, $matches);

输出:

Array
(
    [0] => sec-stunnel-client-*
    [1] => sec-stunnel-crl-*
    [2] => sec-stunnel-loaded-*
    [3] => sec-stunnel-loading-*
    [4] => sec-stunnel-no-*
    ...
)

Code demo