Hi Mike,
First of all, it looks from your example, that you don't necessarily need to
use Regular Expressions at all. Unless there is something you need to match
other than the beginning of the string, you certainly do not need to use
Regular Expressions, and they might, in fact, impede performance.
If all you need to do is match the beginning of the string, first, sort both
lists. Then start with the first item in the user's list, and read all
members of the second list using IndexOf(list1) to determine if that entry
starts with the string from the first list. At the point where it returns 0,
you have found the first match. When it returns -1, you have found the last
match for that entry. The end point is now the starting point of the second
loop though the second list, and you repeat this process until you reach the
end of the first list.
User's Sorted List (listA):
001122
002233
002245
Sorted Available Mac Addresses (listB):
0000000000 - no match
0011112345 - no match
0011234567 - no match
0011223344 - matches [0]
0011223456 - matches [0]
0011234567 - no match
0012345678 - no match
0022334455 - matches [1]
0022334566 - matches [1]
0022345678 - no match
0022346678 - no match
0022455667 - matches [2]
0022456789 - matches [2]
bool[] matches = new bool[listB.Length]
int i = 0, ct = 0;
while (i < listA.Length)
{
while(listB[ct].IndexOf(listA[i]) != 0); // skip non-matches
{
listB[ct] = false;
ct++;
}
while(listB[ct].IndexOf(listA[i]) == 0) // find all matches
{
listB[ct] = true;
ct++:
}
i++;
}
--
HTH,
Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist
Big thicks are made up of lots of little thins.
"Mike Davies" <mike@(cutmeout)scrappy.freeserve.co.uk> wrote in message
news:h5********************************@4ax.com...
Hi Everyone,
Is there a better way of doing the following?
I have 2 lists. List 1 is a list of MAC addresses and List 2 is a list
of regular expressions. A user is only allowed to view devices that
match their list of regular expressions.
Small example:
List1
-------
0011223344
0022334455
0011225544
List2
-------
001122.*
002233.*
Now the only way I can see of doing this is to have a foreach loop for
List 1 and then nest a foreach loop for list2.
Pseudo Code
foreach(item in list1)
{
foreach(item in list2)
{
if (list1.item is a match on list2 regular expression)
{
Write(This device is allowed!)
break;
}
}
}
Seems a very inefficient way of doing it but can;t think of any othe
way to match list1 to list2? BTW, List1 could get quite large and
List2 could be a fair size as well.
Any ideas,
Thanks