【发布时间】:2014-07-22 08:44:31
【问题描述】:
我使用正则表达式从弯括号(或“括号”)中提取数据,例如从(a,b) 中提取a,b,如下所示。我有一个文件,其中每一行都像
this is the range of values (a1,b1) and [b1|a1]
this is the range of values (a2,b2) and [b2|a2]
this is the range of values (a3,b3) and [b3|a3]
我正在使用以下字符串来提取a1,b1、a2,b2 等...
@numbers = $_ =~ /\((.*),(.*)\)/
但是,如果我想从方括号[] 中提取数据,我该怎么做呢?例如
this is the range of values (a1,b1) and [b1|a1]
this is the range of values (a1,b1) and [b2|a2]
我只需要提取/匹配方括号中的数据,而不是弯括号。
【问题讨论】:
-
只需使用@numbers = $_ =~ /[(.*),(.*)]/
-
@Jens @Naidu:请不要只使用那个模式。您将强制引擎进行大量不必要的回溯。
.*几乎从来都不是你真正想要的。
标签: regex perl non-greedy