开发者

a simple Regex question

im trying to learn about regex, and how to use it braking up som logs 开发者_JAVA技巧and populating som textboxes with the results.

if i have a simple line like this

Port status: tro-S-02-av1 0/23

and want to put the tro-S-02-av1 and the 0/23 in a variable

all names wold end on av1 so the regular exspression shold be based on this.

i was thinking like this to try to get the string of tro-S-02-av1 to become the value a text box but i cant get it right, how can i do this.

    Regex r;
    Match m;

    r = new Regex("$`\av1");
    m = r.Match("Port status: tro-S-02-av1 0/23");

    nodetbx.Text = m.Value;


Using groups with the following regex (for example, you could probably clean this up)

Port status: (?<ID>[\d\w\-]+)\s(?<ID2>[\s\S]+)

Will give you two named groups, ID1 and ID2 you can then populate.

I found using named groups easier when first learning regex so you can really see what's going on.

Take a look at nregex to help practice and test regexs too.

    regex r = new regex("Port status: (?<ID>[\d\w\-]+)\s(?<ID2>[\s\S]+)");
    matchcollection mc = r.matches(MyText);
    foreach (match m in mc) {
        string ID1 = m.groups("ID1");
        string ID2 = m.groups("ID2");
    }

Where MyText is either each line looping through the file with ReadLine or the whole file if not too big.

You can then use ID1 and ID2 (renamed to something meaningful) to populate textbox or whatever.


For this particular item you could use the Split string method

string[] words = s.Split(' ');

use the 2nd and 3rd value in the arraylist.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜