gpt4 book ai didi

regex - Perl 拆分和正则表达式

转载 作者:行者123 更新时间:2023-12-04 17:03:41 25 4
gpt4 key购买 nike

我有以下字符串:

'100% California Grown Olives, Water, Salt And Ferrous Gluconate (An,Iron, Derivative),asasd, sadasda'

我正在尝试将其拆分为 /,/但是 仅当 例如,它不在括号内,在这种情况下,结果应该是:
100% California Grown Olives
Water
Salt And Ferrous Gluconate (An,Iron, Derivative)
asasd
sadasda

谢谢,

最佳答案

@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

关于regex - Perl 拆分和正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8481345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com