Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract matching string from large text?

Tags:

c#

regex

mvvm

wpf

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.

like image 836
CodeTheft Avatar asked Dec 17 '14 10:12

CodeTheft


People also ask

How do I extract part of a text string in Excel?

Depending on where you want to start extraction, use one of these formulas: LEFT function - to extract a substring from the left. RIGHT function - to extract text from the right. MID function - to extract a substring from the middle of a text string, starting at the point you specify.

What is Regexextract in Excel?

Extracts the first matching substrings according to a regular expression.

Is RegEx matching fast?

Regular expression matching can be simple and fast, using finite automata-based techniques that have been known for decades. In contrast, Perl, PCRE, Python, Ruby, Java, and many other languages have regular expression implementations based on recursive backtracking that are simple but can be excruciatingly slow.


2 Answers

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.

like image 70
decPL Avatar answered Sep 22 '22 09:09

decPL


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

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

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

like image 30
vks Avatar answered Sep 23 '22 09:09

vks