需要正则表达式来查找特定模式的所有子字符串


Need regular expression to find all substring of a particular pattern

我有一个字符串,可能看起来像这样:

IF([f1.attr_78431_]=1)  THEN(SHOWHIDE([f1.attr_78795_],[f1.attr_78641_];  [f1.attr_78795_])) 
ELSE IF([f1.attr_78431_]=2)  THEN(SHOWHIDE([f1.attr_78798_];[f1.attr_78795_], [f1.attr_78736_]))
ELSE(HIDE([f1.attr_78435_]))

我想找到这些模式的所有子字符串:

SHOWHIDE([f1.attr_78795_],[f1.attr_78641_];  [f1.attr_78795_])
HIDE([f1.attr_78435_])
SHOW([f1.attr_78435_],[f1.attr_78641_])

并忽略所有其他模式,如

IF([f1.attr_78431_]=1)

不知道怎么做。

使用preg_match_all函数。

(?:(?:SHOW)?HIDE|SHOW)'([^')]*')

代码:

$re = "/(?:(?:SHOW)?HIDE|SHOW)''([^'')]*'')/m";
$str = "IF([f1.attr_78431_]=1) THEN(SHOWHIDE([f1.attr_78795_],[f1.attr_78641_]; [f1.attr_78795_])) 'nELSE IF([f1.attr_78431_]=2) THEN(SHOWHIDE([f1.attr_78798_];[f1.attr_78795_], [f1.attr_78736_]))'nELSE(HIDE([f1.attr_78435_]))";
preg_match_all($re, $str, $matches);

演示

试试这个。。。

preg_match_all('/(?:show|hide)+'([^')]+')/i', $str, $matches);