2014-12-08 25 views
2

我编码的perl正则表达式提取某个锚点后的单词, 它似乎不工作。我究竟做错了什么。perl正则表达式 - 锚和模式匹配

这是我的实际输出,我需要提取每一个数字组关键字

$id cuser301 uid=2301(cuser301) gid=32(rpc) groups=32(rpc),1001(cgrp1),1002(cgrp2),1003(cgrp3),1004(cgrp4),1005(cgrp5),1006(cgrp6),1007(cgrp7),1008(cgrp8),1009(cgrp9),1010(cgrp10),1011(cgrp11),1012(cgrp12),1013(cgrp13),1014(cgrp14),1015(cgrp15),1016(cgrp16),1017(cgrp17),1018(cgrp18),1019(cgrp19),1020(cgrp20),1021(cgrp21),1022(cgrp22),1023(cgrp23),1024(cgrp24),1025(cgrp25),1026(cgrp26),1027(cgrp27),1028(cgrp28),1029(cgrp29),1030(cgrp30),1031(cgrp31),1032(cgrp32) 

后,从上面的,我运行id命令,然后想组请帮后捕捉到的数字。

我正在使用以下内容。

my $check_groups = execute("\id $user"); #---> (execute is to run commands on the linux client, please ignore it) 

my $new_groups = ('/^groups/',$check_groups); # ---> Now $new_groups should have all numbers after groups. 
+0

请提供一个样本输出 – 2014-12-08 03:41:42

+0

预期样品输出应该是:32,1001,1002,1003 ......等等 – Rohit 2014-12-08 03:47:55

+0

这是惊人的 - https://regex101.com/r/uZ9tO6/1非常感谢您 – Rohit 2014-12-08 05:29:05

回答

0
my $input = '$id cuser301 uid=2301(cuser301) gid=32(rpc) groups=32(rpc),1001(cgrp1),1002(cgrp2),1003(cgrp3),1004(cgrp4),1005(cgrp5),1006(cgrp6),1007(cgrp7),1008(cgrp8),1009(cgrp9),1010(cgrp10),1011(cgrp11),1012(cgrp12),1013(cgrp13),1014(cgrp14),1015(cgrp15),1016(cgrp16),1017(cgrp17),1018(cgrp18),1019(cgrp19),1020(cgrp20),1021(cgrp21),1022(cgrp22),1023(cgrp23),1024(cgrp24),1025(cgrp25),1026(cgrp26),1027(cgrp27),1028(cgrp28),1029(cgrp29),1030(cgrp30),1031(cgrp31),1032(cgrp32)'; 
print join ',', $input =~ /(?:.*groups=|\G.*?)\b([0-9]+)/g; 

这是一个常见的模式;在更复杂的情况下,如果您想确保\G分支仅适用于第一次非零长度匹配之后,则可以使用\G(?!\A)而不仅仅是\G

+0

伟大的观察。谢谢你 – Rohit 2014-12-08 19:45:11

0

尝试这样做:

$ echo <INPUT> | perl -ne 'print "$1," while /,(\d+)\(/g' 

检查https://regex101.com/r/uZ9tO6/1

+0

留下了32,离开尾随',' – ysth 2014-12-08 07:23:02