很难描述我会做什么。所以我在例子中展示它。
我的字符串:
my $string = q(
min_entry = -0.236, 0, 0.236 , 0.382, 0.500, 0.618, 0.764
max_entry=0.236, 0.382, 0.500, 0.618, 0.764, 1.000
#jakis komentarz
rsi_confirm= 25,27,30, 32
slope3 = 0.236, 0.382, 0.5, 0.764
min_tp=0.0125 , 0.0236, 0.0382, 0.05, 0.0764, 0.1
interval = 14
[thresholds]
low = 40
high = 40
persistence = 9
我的比赛模式:
my @match = $string =~ /(([\d-\.]+[, ]+)+[\d-\.]+)/sg;
print Dumper \@match;
我的结果:
$VAR1 = [
'-0.236, 0, 0.236 , 0.382, 0.500, 0.618, 0.764',
'0.618, ',
'0.236, 0.382, 0.500, 0.618, 0.764, 1.000',
'0.764, ',
'25,27,30, 32',
'30, ',
'0.236, 0.382, 0.5, 0.764',
'0.5, ',
'0.0125 , 0.0236, 0.0382, 0.05, 0.0764, 0.1',
'0.0764, '
];
我不知道为什么或如何使用索引1(值'0.618,',),3(值'0.764,',),5,7,9添加我的正则表达式。但我不需要它。
结果我想实现:
print Dumper \@match;
$VAR1 = [
'-0.236, 0, 0.236 , 0.382, 0.500, 0.618, 0.764',
'0.236, 0.382, 0.500, 0.618, 0.764, 1.000',
'25,27,30, 32',
'0.236, 0.382, 0.5, 0.764',
'0.0125 , 0.0236, 0.0382, 0.05, 0.0764, 0.1',
]
请以我的正则表达为基础回答。标识字符的唯一重复字符串是“=”或“=”(在模式之前)和“,”(在模式的中间)
答案 0 :(得分:1)
您有两个括号组,一个在另一个内。内在的每一个结果都会产生。您应该使用a non-capturing group进行内部分组。
答案 1 :(得分:1)
您希望使用群集将正则表达式的这些部分组合在一起,而不是使用捕获组。通过执行(?:whatever)
而不是(whatever)
来完成群集,因此您的代码将变为......
my @match = $string =~ /(?:(?:[\d-\.]+[, ]+)+[\d-\.]+)/sg;
答案 2 :(得分:1)
猜测一下,这个字符串是您完整阅读的文件的内容,以使事情变得更容易"。不幸的是,这意味着你必须明确地迎合新行字符,这会使事情变得很复杂
以下是使用DATA
文件句柄执行操作的示例。 Buliding @aoa
简化为单个语句。当然你可以打开一个文件并使用其中的句柄
代码中的错误导致只有一个数字(并且没有逗号)的行被忽略。您可能需要这种行为,但我已经修复了#34;它在这里
use strict;
use warnings 'all';
my @aoa = map { /-?\d+(?:\.\d+)?/g } <DATA>;
use Data::Dumper;
print Dumper \@aoa;
__DATA__
min_entry = -0.236, 0, 0.236 , 0.382, 0.500, 0.618, 0.764
max_entry=0.236, 0.382, 0.500, 0.618, 0.764, 1.000
#jakis komentarz
rsi_confirm= 25,27,30, 32
slope3 = 0.236, 0.382, 0.5, 0.764
min_tp=0.0125 , 0.0236, 0.0382, 0.05, 0.0764, 0.1
interval = 14
[thresholds]
low = 40
high = 40
persistence = 9
$VAR1 = [
[
'-0.236',
'0',
'0.236',
'0.382',
'0.500',
'0.618',
'0.764'
],
[
'0.236',
'0.382',
'0.500',
'0.618',
'0.764',
'1.000'
],
[],
[
'25',
'27',
'30',
'32'
],
[
'3',
'0.236',
'0.382',
'0.5',
'0.764'
],
[
'0.0125',
'0.0236',
'0.0382',
'0.05',
'0.0764',
'0.1'
],
[
'14'
],
[],
[],
[
'40'
],
[
'40'
],
[
'9'
]
];
我还怀疑即使这不是解决问题的最佳方法,因为您丢弃了所有数据标签,因此您不知道哪个数字列表属于哪个类别除了位置
此替代方法构建数组的散列,以便保留该值
use strict;
use warnings 'all';
my %data;
while ( <DATA> ) {
next unless /=/;
my ($key, @values) = /[-\w.]+/g;
$data{$key} = \@values;
}
use Data::Dumper;
print Dumper \%data;
__DATA__
min_entry = -0.236, 0, 0.236 , 0.382, 0.500, 0.618, 0.764
max_entry=0.236, 0.382, 0.500, 0.618, 0.764, 1.000
#jakis komentarz
rsi_confirm= 25,27,30, 32
slope3 = 0.236, 0.382, 0.5, 0.764
min_tp=0.0125 , 0.0236, 0.0382, 0.05, 0.0764, 0.1
interval = 14
[thresholds]
low = 40
high = 40
persistence = 9
$VAR1 = {
'high' => [
'40'
],
'interval' => [
'14'
],
'slope3' => [
'0.236',
'0.382',
'0.5',
'0.764'
],
'persistence' => [
'9'
],
'low' => [
'40'
],
'min_tp' => [
'0.0125',
'0.0236',
'0.0382',
'0.05',
'0.0764',
'0.1'
],
'min_entry' => [
'-0.236',
'0',
'0.236',
'0.382',
'0.500',
'0.618',
'0.764'
],
'max_entry' => [
'0.236',
'0.382',
'0.500',
'0.618',
'0.764',
'1.000'
],
'rsi_confirm' => [
'25',
'27',
'30',
'32'
]
};
在不了解完整问题的情况下,这是我能为您做的最好的