9

我想提取行键(这里是28_2820201112122420516_000000)、列名(这里是bcp_startSoc)和值(这里是64.0$str,其中$str是 HBase 中的一行:

# `match` is OK
my $str = '28_2820201112122420516_000000 column=d:bcp_startSoc, timestamp=1605155065124, value=64.0';
my $match = $str.match(/^ ([\d+]+ % '_') \s 'column=d:' (\w+) ',' \s timestamp '=' \d+ ',' \s 'value=' (<-[=]>+) $/);
my @match-result = $match».Str.Slip;
say @match-result;   # Output: [28_2820201112122420516_000000 bcp_startSoc 64.0]

# `smartmatch` is OK
# $str ~~ /^ ([\d+]+ % '_') \s 'column=d:' (\w+) ',' \s timestamp '=' \d+ ',' \s 'value=' (<-[=]>+) $/
# say $/».Str.Array; # Output: [28_2820201112122420516_000000 bcp_startSoc 64.0]

# `comb` is NOT OK
# A <( token indicates the start of the match's overall capture, while the corresponding )> token indicates its endpoint. 
# The <( is similar to other languages \K to discard any matches found before the \K.
my @comb-result = $str.comb(/<( [\d+]+ % '_' )> \s 'column=d:' <(\w+)> ',' \s timestamp '=' \d+ ',' \s 'value=' <(<-[=]>+)>/);
say @comb-result;    # Expect: [28_2820201112122420516_000000 bcp_startSoc 64.0], but got [64.0]

我想comb跳过一些匹配,只匹配我想要的,所以我在这里使用多个<()>,但只得到最后一个匹配作为结果。

是否可以comb用来获得与方法相同的结果match

4

3 回答 3

4

TL;DR Multiple <(...)>s 并不意味着多次捕获。即使他们这样做了,.comb也会将每个匹配项减少到它返回的字符串列表中的单个字符串。如果您真的想使用.comb,一种方法是返回到您的原始正则表达式,但也使用正则表达式中的附加代码存储所需的数据

多个<(...)>s 并不意味着多次捕获

正则表达式整体匹配的默认起点是正则表达式的开始。默认终点是终点。

写入<(会将整体匹配的起点重置为您插入它的位置。每次插入一个并在处理正则表达式期间应用它时,它都会重置起点。同样)>重置终点。在处理正则表达式结束时,开始和结束的最终设置将应用于构建最终的整体匹配。

鉴于您的代码只是无条件地重置每个点三次,最后一次开始和结束重置“赢”。

.comb将每个匹配项减少为单个字符串

foo.comb(/.../)相当于foo.match(:g, /.../)>>.Str;

这意味着对于正则表达式的每次匹配,您只能得到一个字符串。

一种可能的解决方案是使用@ohmycloudy 在他们的答案中显示的方法。

但这伴随着我自己和@jubilatious1 在对他们的回答的评论中提出的警告。

添加{ @comb-result .push: |$/».Str }到正则表达式

您可以解决方法.comb的正常运行。我并不是说这是一件好事。我也不是说不是。你问,我在回答,就是这样。:)

从与其他解决方案一起使用的原始正则表达式开始。

然后添加{ @comb-result .push: |$/».Str }到正则表达式的末尾以存储每个匹配的结果。现在你会得到你想要的结果。

于 2020-11-20T01:20:51.407 回答
3
$str.comb( /  ^ [\d+]+ % '_' | <?after d\:> \w+  | <?after value\=> .*/ )
于 2020-11-19T11:30:38.747 回答
2

由于您有一个逗号分隔的“行”信息正在检查,您可以尝试使用split()来分解您的匹配项,并分配给一个数组。在 Raku REPL 下方:

> my $str = '28_2820201112122420516_000000 column=d:bcp_startSoc, timestamp=1605155065124, value=64.0';
28_2820201112122420516_000000 column=d:bcp_startSoc, timestamp=1605155065124, value=64.0
> my @array = $str.split(", ")
[28_2820201112122420516_000000 column=d:bcp_startSoc timestamp=1605155065124 value=64.0]
> dd @array
Array @array = ["28_2820201112122420516_000000 column=d:bcp_startSoc", "timestamp=1605155065124", "value=64.0"]
Nil
> say @array.elems
3

匹配数组的单个元素:

> say @array[0] ~~ m/ ([\d+]+ % '_') \s 'column=d:' (\w+) /;
「28_2820201112122420516_000000 column=d:bcp_startSoc」
 0 => 「28_2820201112122420516_000000」
 1 => 「bcp_startSoc」
> say @array[0] ~~ m/ ([\d+]+ % '_') \s 'column=d:' <(\w+)> /;
「bcp_startSoc」
 0 => 「28_2820201112122420516_000000」
> say @array[0] ~~ m/ [\d+]+ % '_'  \s 'column=d:' <(\w+)> /;
「bcp_startSoc」

对一个或多个数组元素匹配的布尔测试:

> say True if ( @array[0] ~~ m/ [\d+]+ % '_'  \s 'column=d:' <(\w+)> /)
True
> say True if ( @array[2] ~~ m/ 'value=' <(<-[=]>+)> / )
True
> say True if ( @array[0] ~~ m/ [\d+]+ % '_'  \s 'column=d:' <(\w+)> /) & ( @array[2] ~~ m/ 'value=' <(<-[=]>+)> / )
True

HTH。

于 2020-11-19T20:40:21.620 回答