简体   繁体   English

从字符串中提取一些参数

[英]Extract some parameters from string

How can I extract some parameters from two string and populate the datagrid by them ?如何从两个字符串中提取一些参数并通过它们填充数据网格?

I have the config.txt file and there are two strings are repeated in pairs:我有 config.txt 文件,有两个字符串成对重复:

set interface "ethernet1/1.271" tag 271 zone "Ntg-Gom"
set interface ethernet1/1.271 ip 192.168.9.6/30

From this two string i need in extracting ethernet1/1.271 , 271 , 192.168.9.6/30.从这两个字符串中我需要提取 ethernet1/1.271 、 271 、 192.168.9.6/30。 All this for populating such datagrid:所有这些都是为了填充这样的数据网格:

在此处输入图片说明

Obviously, I need a regular expression.显然,我需要一个正则表达式。 Now I have the regexp for one parameter (here ethernet):现在我有一个参数的正则表达式(这里是以太网):

StreamReader reader2 = new StreamReader(opendialog.FileName);
string patternI = @"set interface (""ethernet\S+"")";

var matchesI =
Regex.Matches(reader2.ReadToEnd(), patternI).Cast<Match>().Where(m => m.Success)
                        .Select(m => m.Groups[1].Value);

How to construct more complex regexp I do not know!我不知道如何构造更复杂的正则表达式! wasting a lot of time...浪费很多时间...

This will probably do it:这可能会做到:

matchResults = Regex.Match(
                 subjectString, 
                 @"""(.*?)""\s+tag\s+(\d+).*?ip\s+([\d./]+)", 
                 RegexOptions.Singleline);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM