简体   繁体   English

正则表达式匹配,排除某些模式

[英]Regex match that exclude certain pattern

I want to split the string around for comma( , ) or & . 我想围绕分割字符串逗号( , )或& This is simple but I want to stop the match for any content between brackets. 这很简单,但是我想停止括号之间任何内容的匹配。 For example if I run on 例如,如果我在

sleeping , waking(sit,stop)

there need to be only one split and two elements 只需要一个拆分和两个元素

thanks in advance 提前致谢

This is a perfect example for the (*SKIP)(*FAIL) mechanism PCRE (and thus PHP ) offers. 这是PCRE (以及PHP )提供的(*SKIP)(*FAIL)机制的完美示例。
You could come up with the following code: 您可以提出以下代码:

<?php
$string = 'sleeping , waking(sit,stop)';
$regex = '~\([^)]*\)(*SKIP)(*FAIL)|[,&]~';
# match anything between ( and ) and discard it afterwards
# instead match any of the characters found on the right in square brackets
$parts = preg_split($regex, $string);
print_r($parts);
/*
Array
(
    [0] => sleeping 
    [1] =>  waking(sit,stop)
)
*/

?>

This will split any , or & which is not in parentheses. 这将分割任何不在括号中的,&

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM