Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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
Regex *未正确捕获逗号_Regex_Ruby - Fatal编程技术网

Regex *未正确捕获逗号

Regex *未正确捕获逗号,regex,ruby,Regex,Ruby,我的正则表达式不能正确处理一行中的逗号 if line =~ /^.*,(\d{4}-\d{2}-\d{2}),(\d+:\d+),.*,(\d+),.*,(.*) && (.*),Other,Other.*$/ 我正在逐行读取一个文件,该文件应如下所示: "",2019-06-14,01:30,,27,"",Episode Name && Series Name,Other,Other,LOCAL 但是,如果序列名中有逗号,例如“Busy Electron,

我的正则表达式不能正确处理一行中的逗号

if line =~ /^.*,(\d{4}-\d{2}-\d{2}),(\d+:\d+),.*,(\d+),.*,(.*) && (.*),Other,Other.*$/
我正在逐行读取一个文件,该文件应如下所示:

"",2019-06-14,01:30,,27,"",Episode Name && Series Name,Other,Other,LOCAL

但是,如果序列名中有逗号,例如“Busy Electron,the”,则正则表达式将只捕获“the”,而不是整个名称。

这可能是我们在此搜索的表达式,但不是正确的方法:

"(.*?)",([0-9]{4}-[0-9]{2}-[0-9]{2})\s*,([0-9]{2}:[0-9]{2}),(.*?),([0-9]+),"(.*?)",(.*?),(.*?),(.*?),(.*)
不包括最后一个字段,我们只需为那些不需要验证的字段实现一个惰性量词
(.*)

(.*)
不起作用,因为它会使整个捕获组变懒,而不是
*

在此之前,它将到达
*
已经将我们的字符串刷到了末尾,并收集了除换行符以外的所有字符

如果我们希望增加或减少限制,我们可以这样做,例如:

"(.*?)",\s*([0-9]{4}-[0-9]{2}-[0-9]{2})\s*,\s*([0-9]{2}:[0-9]{2})\s*,(.*?),\s*([0-9]+)\s*,"(.*?)",(.*?),(.*?),(.*?),(.*)
试验 正则表达式电路 可视化正则表达式:


理想情况下,我可以捕获所有字段,以便捕获的插曲名称为“Busy Electron,the”,但目前,正则表达式仅捕获“the”。无论何时给出示例,都应始终将所需结果显示为有效的Ruby对象。此外,所有数据输入都应该是有效的Ruby对象(您需要用单引号括住字符串),并且您应该为每个对象分配一个变量,以便读者可以在回答中引用这些变量(例如,
line
)。大概,
line='”,2019-06-14,01:30,27,“,集名和系列名,其他,其他,本地'
。所需结果是否为包含日期、时间等的数组?请根据需要编辑。非常感谢!我想我需要的是:(.*),([0-9]{4}-[0-9]{2}-[0-9]{2}]s*,([0-9]{2}],(.*),([0-9]+),(.*),(.*)&&(.*),(.*),因为“&&&&&”应该是插曲和系列名称之间的分隔符,但并非所有插曲/系列名称中都有逗号。我不太明白为什么会这样,你能解释一下为什么“(.*)”有效,但“(.*)”无效吗?只是出于我自己的好奇心。
re = /"(.*?)",([0-9]{4}-[0-9]{2}-[0-9]{2})\s*,([0-9]{2}:[0-9]{2}),(.*?),([0-9]+),"(.*?)",(.*?),(.*?),(.*?),(.*)/m
str = '"",2019-06-14,01:30,,27,"",Episode Name && Series Name,Other,Other,LOCAL

"some things we wish here",2019-06-14,01:30,some things we wish here,27,"some things we wish here",Episode Name && Series Name,Other,Other,LOCAL'

# Print the match result
str.scan(re) do |match|
    puts match.to_s
end