我有以下字符串:
'100% California Grown Olives, Water, Salt And Ferrous Gluconate (An,Iron, Derivative),asasd, sadasda'
我想把它分开/,/
but only if例如,它不在括号内,在这种情况下结果应该是:
100% California Grown Olives
Water
Salt And Ferrous Gluconate (An,Iron, Derivative)
asasd
sadasda
thanks,
@result = split(m/,(?![^()]*\))/, $subject);
仅当下一个括号(如果有)不是右括号时,才会以逗号分隔。正如 Jack Maney 正确指出的那样,如果可能出现嵌套括号,这可能会导致失败。
解释:
, # Match a comma.
(?! # Assert that it's impossible to match...
[^()]* # any number of non-parenthesis characters
\) # followed by a closing parenthesis
) # End of lookahead assertion
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)