2014-12-08 58 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