Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to parse PnPID

Tags:

c#

regex

I want to get the value of vendor ID and device ID from a PnPID, for example, I want to get the vendor ID "8086" and device ID "24D5" from the below string.

pci\ven_8086&dev_24D5&subsys_02871014

my code is (in C#)

Regex rx = new Regex(@"dev_\d+", RegexOptions.IgnoreCase);
string text = @"pci\ven_8086&dev_2425&subsys_02871014";
MatchCollection matches = rx.Matches(text);
foreach (Match match in matches)
{
    Console.WriteLine(watch);
}

But this does not remove the prefixing "dev_" for me, and it can not match a device ID which contains hexdecimal digits. What is the right expression?

like image 445
kennyzx Avatar asked Sep 01 '26 14:09

kennyzx


1 Answers

You have to create a group using parentheses:

new Regex(@"dev_(\d+)", RegexOptions.IgnoreCase);

Then in second group you will get just digits (first is always reserved for string that is matched by whole regex).

To match hexdecimal value use:

new Regex(@"dev_([0-9a-f]+)", RegexOptions.IgnoreCase);

To match both - device and vendor - in one regex, use:

new Regex(@"ven_([0-9a-f]+)&dev_([0-9a-f]+)", RegexOptions.IgnoreCase);
like image 137
hsz Avatar answered Sep 03 '26 04:09

hsz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!