简体   繁体   中英

How to extract matching string from large text?

I am working on a project based on WPF,C# and MVVM . Its basically a networking device configurable application via telnet.I have a following output in my wpf textbox and I want to extract MAC Address column values.

active500EM#sh mac-address-table
Read mac address table....
Vlan Mac Address                 Type    Creator   Ports
---- --------------------------- ------- -----------------------
1    00-23-8b-87-9a-6b           DYNAMIC Hardware Ethernet1/0/12
1    00-8c-fa-72-94-b1           DYNAMIC Hardware Ethernet1/0/1
1    3c-43-8e-5c-3e-05           DYNAMIC Hardware Ethernet1/0/8
1    d0-59-e4-b9-e9-3e           DYNAMIC Hardware Ethernet1/0/8
1    f8-f7-d3-00-03-c0           DYNAMIC Hardware Ethernet1/0/8
1    f8-f7-d3-00-03-f0           STATIC  System   CPU
active500EM#

I think i cannot use regex because i dont have anything to match.Any help and suggestions would be greatly appreciable.

What's wrong with using a regex?

\b(?<mac_addr>([0-9a-f]{2}-){5}[0-9a-f]{2})\b

Then you can use:

var allMacs = Regex.Matches(YOUR_TEXT, REGEX_PATTERN)
                   .Select(m => m.Groups["mac_addr"].Value)
                   .ToList();

to get a List<String> with all matched MAC addresses.

^\d+\s+(\S+)

You can try this.Grab the capture or group.See demo.

https://regex101.com/r/eZ0yP4/32

这是快速的正则表达式

(\w|\d){2}-(\w|\d){2}-(\w|\d){2}-(\w|\d){2}-(\w|\d){2}-(\w|\d){2}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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