Regex 在perl中从字符串中提取子字符串
我有一个字符串,如下所示:Regex 在perl中从字符串中提取子字符串,regex,perl,Regex,Perl,我有一个字符串,如下所示: downCircuit received;TOKENS START;{"action":'"UPDATE","device_id":"CP0027829","link_index":"101","name":"uplink101","description":"link1-0/0/3","priority":"200","status":"DOWN","wan_status":"DOWN","vlan":"4094","vlan_description":"vlan4
downCircuit received;TOKENS START;{"action":'"UPDATE","device_id":"CP0027829","link_index":"101","name":"uplink101","description":"link1-0/0/3","priority":"200","status":"DOWN","wan_status":"DOWN","vlan":"4094","vlan_description":"vlan4094-intf","topic":"uplinks","stream_timestamp":"1547015547","aws_host":"attwifi-poc-central.arubathena.com","aws_timestamp":"1547015547","customer_id":"6666778917"};TOKENS END
我想从中提取link_index的值。i、 e.在这种情况下,输出应为101。有人能帮我从字符串中提取101吗。你可以使用这样一个简单的正则表达式:
"link_index":"(\d+)"
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use JSON;
my $input = 'downCircuit received;TOKENS START;{"action":"UPDATE","device_id":"CP0027829","link_index":"101","name":"uplink101","description":"link1-0/0/3","priority":"200","status":"DOWN","wan_status":"DOWN","vlan":"4094","vlan_description":"vlan4094-intf","topic":"uplinks","stream_timestamp":"1547015547","aws_host":"attwifi-poc-central.arubathena.com","aws_timestamp":"1547015547","customer_id":"6666778917"};TOKENS END';
$input =~ s/.*START;//;
$input =~ s/;TOKENS END//;
my $data = JSON->new->decode($input);
say $data->{link_index};
然后从捕获组中获取内容
my $str = 'downCircuit received;TOKENS START;{"action":\'"UPDATE","device_id":"CP0027829","link_index":"101","name":"uplink101","description":"link1-0/0/3","priority":"200","status":"DOWN","wan_status":"DOWN","vlan":"4094","vlan_description":"vlan4094-intf","topic":"uplinks","stream_timestamp":"1547015547","aws_host":"attwifi-poc-central.arubathena.com","aws_timestamp":"1547015547","customer_id":"6666778917"};TOKENS END';
my $regex = qr/"link_index":"(\d+)"/mp;
if ( $str =~ /$regex/g ) {
print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... and so on\n";
}
您可以使用反向引用:
打印$1,“\n”如果/“链接索引”:“(\d+)/
全面地说:
$string=q(downCircuit received;TOKENS START;{"action":'"UPDATE","device_id":"CP0027829","link_index":"101","name":"uplink101","description":"link1-0/0/3","priority":"200","status":"DOWN","wan_status":"DOWN","vlan":"4094","vlan_description":"vlan4094-intf","topic":"uplinks","stream_timestamp":"1547015547","aws_host":"attwifi-poc-central.arubathena.com","aws_timestamp":"1547015547","customer_id":"6666778917"};TOKENS END);
print $1,"\n" if $string =~ /"link_index":"(\d+)"/;
我有一根像下面这样的线
这里有一些JSON,前后都有额外的粗糙。因此,与其纠结于正则表达式,最好的办法是提取实际的JSON,然后使用它来处理它。大概是这样的:
"link_index":"(\d+)"
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use JSON;
my $input = 'downCircuit received;TOKENS START;{"action":"UPDATE","device_id":"CP0027829","link_index":"101","name":"uplink101","description":"link1-0/0/3","priority":"200","status":"DOWN","wan_status":"DOWN","vlan":"4094","vlan_description":"vlan4094-intf","topic":"uplinks","stream_timestamp":"1547015547","aws_host":"attwifi-poc-central.arubathena.com","aws_timestamp":"1547015547","customer_id":"6666778917"};TOKENS END';
$input =~ s/.*START;//;
$input =~ s/;TOKENS END//;
my $data = JSON->new->decode($input);
say $data->{link_index};
正如预期的那样,这将生成输出101
注意:我想你的问题有错。至少,JSON中存在语法错误。我删除了您在“更新”之前使用的一个不匹配的引号字符,我尝试了此操作,但无效$LinkIndex=~/“link\u index”:“(\d+)/@Bindu转到并复制/粘贴我的代码。它很好用,你可以在那里测试。@Bindu很高兴它有效,记得把问题标记为已解决。你试过什么?什么不起作用?你得到了什么?你期待什么?你的代码有哪些地方不可用?它在哪里?