如何正则表达式匹配a / b / c ...? [英] How to regex match a/b/c ...?
问题描述
如果我有一个像下面这样的数组,元素的顺序是随机的,并且每个元素可以有多少个级别没有限制。这里只显示3个关卡。 a
, b
和 c
。
我希望能够解析这样的数组,并将结果存储在哈希散列中,就像这样,当只有3个层次时
$ VAR1 = {
'a'=> {
'b'=> 'c'
}
};
问题 我的问题是如何为此写一个正则表达式,因为第一级在最后没有 值如何解决这样的问题? 通常情况下,您将分配或修改DiveRef返回的标量引用(或赋值到DiveVal的结果),但在这里你只是想确定键是否存在。 If I have an array like below, where the order of the elements are random and there are no limit on how many levels each element can have. Here only show 3 levels. I would like to be able to parse such arrays, and store the result in a hash of hash like so, when there are only 3 levels Question My problem is how to write a regex for this, because the first level doesn't have a How so solve such a problem?
Normally you would assign to or modify the scalar reference returned by DiveRef (or assign to the result of DiveVal), but here you just want to make sure the key exists. The 这篇关于如何正则表达式匹配a / b / c ...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
/
,并且因为元素的顺序是随机的,如果 a / b / c
已经插入散列,那么元素 a
不应该删除散列键 a
。
#!/ usr / bin / perl
use strict;
使用警告;
使用Data :: Dumper;
my @ar =(a,a / b,a / b / c);
my%h =();
foreach my $ a(@ar){
}
#!/ usr / bin / perl
use strict;
使用警告;
使用Data :: Dumper;
使用Data :: Diver'DiveRef';
我的@array =(a,a / b,a / b / c);
my%hash =();
foreach my $元素(@array){
DiveRef(\%hash,\(split / \ //,$ element));
}
\(...)
将分割返回的列表转换为标量引用列表,它告诉Data :: Diver它们总是散列键,如果它们是数字,则不可能是数组索引。a
, b
, and c
.$VAR1 = {
'a' => {
'b' => 'c'
}
};
/
at the end, and since the order of the elements is random, if a/b/c
have already been inserted in the hash, then the element a
shouldn't delete the hash values for key a
.#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my @ar = ("a", "a/b", "a/b/c");
my %h = ();
foreach my $a (@ar) {
}
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use Data::Diver 'DiveRef';
my @array = ("a", "a/b", "a/b/c");
my %hash = ();
foreach my $element (@array) {
DiveRef( \%hash, \( split /\//, $element ) );
}
\(...)
turns the list returned by split into a list of scalar references, which tells Data::Diver these are always going to be hash keys, not possibly array indexes if they are numeric.