How To Compare Two Lists Using Regex?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How To Compare Two Lists Using Regex?



I Have Two Lists,



1. List<string>IDList //DM-0016 (ID:225),LPG Test (ID:232), 18S1(ID:290)..
2List<string>Names // DM-0016,LPG Test, 18S1..


List<string>IDList //DM-0016 (ID:225),LPG Test (ID:232), 18S1(ID:290)..


List<string>Names // DM-0016,LPG Test, 18S1..



The Second List is basically the same as the First but it doesn't have the IDs.
I am Showing the Second List in a Multiple Choice Dialog, Where, user can select multiple items.



I have a Regular Expression (@"(?<=(ID:)[0-9]+"); This returns only the the number inside (ID:)
I want to compare these two Lists based on this RegEX, Where, I want only the ID's of the selected items.


(@"(?<=(ID:)[0-9]+");



For Ex: If DM-0061 is selected i'll get 225 like so.





what have you tried so far
– Lucifer
6 mins ago





What about splitting the strings into arrays (split on ","), then looping over the list with IDs first to map the names to IDs, then looping over the names and find the ID per name from the other array?
– minitauros
1 min ago




1 Answer
1



If you use a regular expression such as:


(?<key>.*?)s(ID:(?<value>[0-9]+))



You can project your original list to a dictionary and easily use it as a lookup:


var list = new List<string>(){
"DM-0016 (ID:225)","LPG Test (ID:232)", "18S1 (ID:290)"
};
var regex = new Regex(@"(?<key>.*?)s(ID:(?<value>[0-9]+))");
var dict = list.Select(i => regex.Match(i)).ToDictionary(k => k.Groups["key"].Value, v => v.Groups["value"].Value);
Console.WriteLine(dict["DM-0016"]); // writes 225



Live example: http://rextester.com/PMTUFW14656






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived