Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 匹配逗号前字符串的正则表达式,除非文本包含也由逗号分隔的扣合列表_Ruby_Regex_Lookahead - Fatal编程技术网

Ruby 匹配逗号前字符串的正则表达式,除非文本包含也由逗号分隔的扣合列表

Ruby 匹配逗号前字符串的正则表达式,除非文本包含也由逗号分隔的扣合列表,ruby,regex,lookahead,Ruby,Regex,Lookahead,假设这是我的文本: "a, b, c (1, 2, 3), d, f (x, y)" 我希望匹配枚举的每个点,但我也不希望子列表逗号被解释为终止字符。我需要一个正则表达式来提取以下匹配项: a b c (1,2,3) d f (x, y) 我尝试使用一个可选的lookahead,但我无法让它工作。您可以使用string.split函数 > "a, b, c (1, 2, 3), d, f (x, y)".split(/,\s*(?![^()]*\))/) => ["a", "b"

假设这是我的文本:

"a, b, c (1, 2, 3), d, f (x, y)"
我希望匹配枚举的每个点,但我也不希望子列表逗号被解释为终止字符。我需要一个正则表达式来提取以下匹配项:

a
b
c (1,2,3)
d
f (x, y)

我尝试使用一个可选的lookahead,但我无法让它工作。

您可以使用
string.split
函数

> "a, b, c (1, 2, 3), d, f (x, y)".split(/,\s*(?![^()]*\))/)
=> ["a", "b", "c (1, 2, 3)", "d", "f (x, y)"]
上述正则表达式将匹配所有逗号和以下零个或多个空格,前提是它后面没有(
[^()]*
任何字符,但后面没有
零次或多次以及右大括号