Trying to do a match between two strings when both the strings have * wildcards. E.g. license plates witness 1 sees A*23* and withness 2 sees *123*Z. I want these to flag up as a match.
e.g. if I niavely do
string plate = "A.*23.*";
Regex r1 = new Regex(@".*3.*");
Console.WriteLine(r1.IsMatch(plate));
Regex r2 = new Regex(@"A123");
Console.WriteLine(r2.IsMatch(plate));
Regex r3 = new Regex(@".*Z");
Console.WriteLine(r3.IsMatch(plate));
Regex r4 = new Regex(@".*123.*Z.*");
Console.WriteLine(r4.IsMatch(plate));
then I get
true
false
false
false
whereas I want them all to be true. The problem is a bit more complex than this, but understanding how to deal with wildcard in the original string (not just the regex) would be a good start. Any ideas?
Loading
Bruce RoeserPosted Jul 27, 2010, 12:50 PM
Glad to hear you found the solution. RegEX is pretty sophisticated but I was beginning to think the same thing you were - that it's just too general-purpose for your needs.
-bruce
stevePosted Jul 27, 2010, 12:05 PM
http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/55fed3c4-57d0-49e0-83fa-a26025043fe8
stevePosted Jul 27, 2010, 10:09 AM
Many thanks for your interest in this. Basically the problem is comparing two strings where either or both strings can have multiple * wildcards.
The four expressions in my first post are just examples, was just saying that A*23* , *3*, *Z, *123*Z etc could all refer to the same (unknown) string A123XYZ (for example). In my application, A*23* is what is recorded in a database, the other four example were searches that should come back as a match.
. Eventually there will be other restrictions (such as a max of 3 numbers in the string).. but now thinking that regex is not going to be any easier than working out a recursive algorithm from scratch. Unless you or someone else has a cunning plan... I'm fresh out!
Cheers, Steve
Bruce RoeserPosted Jul 27, 2010, 9:21 AM
I'm in the process of studying RegEx myself so I spent some time tinkering with this yesterday.
Before diving too deeply into this, are you sure you mean to be matching one license plate against 4 separate RegEx expressions or do you really mean that you want one RegEx expression for processing multiple plates? If it's the latter then your example is backwards: you would need to develop one RegEx expression to match multiple plates against. I.E.
Regex r = new RegEx( "A.*23.*");
Console.WriteLine(r.IsMatch(plate1));
...
Console.WriteLine(r.IsMatch(plateN));
Let me know. I'd like to work through this one too, as an exercise for myself as well as helping you figure it out.
-Bruce :-)