none
提取字符串 C# RRS feed

  • 问题

  • 有如下一段英文, 我如何写一个正则表达式提取出

    access-list 110689 extended permit ip any object-group OA

    access-list 110689 extended permit ip any object-group ERP

    access-list 110689 extended permit ip any object-group QQ

    这三句话放入List<string>里面。

    请问各位大侠有好的方法吗?

    login as: aa
    admin@172.220.22.38's password:bb
    User admin logged in to dgvpn 
    dgvpn> enable
    Password:cc
    dgvpn# configure terminal
    dgvpn(config)# show running-config access-list 110689
    access-list 110689 extended permit ip any object-group OA

    access-list 110689 extended permit ip any object-group ERP

    access-list 110689 extended permit ip any object-group QQ

    dgvpn(config)# no group-policy 11068966 attributes

    2018年6月30日 1:56

答案

  • 你好,

    正如懒得勤快所说, 使用正则表达式来做,下面是一个完整的例子,你可以参考一下:

    var test = @"login as: aa
                            admin@172.220.22.38's password:bb
                            User admin logged in to dgvpn 
                            dgvpn> enable
                            Password:cc
                            dgvpn# configure terminal
                            dgvpn(config)# show running-config access-list 110689
                            access-list 110689 extended permit ip any object-group OA
    
                            access-list 110689 extended permit ip any object-group ERP
    
                            access-list 110689 extended permit ip any object-group QQ
    
                            dgvpn(config)# no group-policy 11068966 attributes";
    
                string regText = @"access-list \d+ extended permit ip any object-group \w+";
                Regex regex = new Regex(regText);
                Match result = regex.Match(test);
    
                MatchCollection mc = regex.Matches(test);
                List<string> list = new List<string>();
                foreach (var item in mc)
                {
                    list.Add(item.ToString());
                }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年7月2日 8:41
    版主

全部回复

  • 正则表达式如下:

    access-list \d+ extended permit ip any object-group \w+

    其实就是只需要把中间的数字和最后的字母用正则匹配上就行了

    2018年7月1日 12:40
  • 你好,

    正如懒得勤快所说, 使用正则表达式来做,下面是一个完整的例子,你可以参考一下:

    var test = @"login as: aa
                            admin@172.220.22.38's password:bb
                            User admin logged in to dgvpn 
                            dgvpn> enable
                            Password:cc
                            dgvpn# configure terminal
                            dgvpn(config)# show running-config access-list 110689
                            access-list 110689 extended permit ip any object-group OA
    
                            access-list 110689 extended permit ip any object-group ERP
    
                            access-list 110689 extended permit ip any object-group QQ
    
                            dgvpn(config)# no group-policy 11068966 attributes";
    
                string regText = @"access-list \d+ extended permit ip any object-group \w+";
                Regex regex = new Regex(regText);
                Match result = regex.Match(test);
    
                MatchCollection mc = regex.Matches(test);
                List<string> list = new List<string>();
                foreach (var item in mc)
                {
                    list.Add(item.ToString());
                }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年7月2日 8:41
    版主
  • 牛B    谢谢大侠帮忙
    2018年7月9日 6:05