This code is for matching a string with a wildcard in it to another string. If you want to have a Astrix in the first string be counted as a character and not as a wildcard put a \ in front of it. I have tested it for quite a few different possibilities and it has worked for what I have tested it with. Include is a short main function demonstrating it working. Fell free to use it if you want but I CAN'T guarantee that it is bug free.
That will not compile -- if you want a \ in the string then you have to put two of them, so the code should be searchFor = "*my\\*day.*";
Otherwise the program appears to work ok for the tests I made. Now I think you should improve the program by allowing ? as a wild card that matches just a single character.
I think there is some problems..
I tried the escaping of asterisk - didn't seem to work.
I tried to searchFor "*.php" and set word to "MyScript.php" - returned false.
Thank you AD for your reply. The single slash in my code was a typo. As for having a ? act as a any single letter I am implementing that right now and I should have it finished shortly. I'll post the updated code once its done.
@ Excizted I fixed what was causing the problem and when I post the updated code "*.php" for searchFor and "MyScript.php" for word will work.
Okay well I believe I have it all sorted out. I added using a ? as a single letter wildcard and it appears to work just fine. I wound up having to write another function that compared 2 strings if one had a ? in it. I just went through the string that had the ?'s in it and where ever there was a ? i replaced it with the letter in the second string at that same spot. The main function has not changed from my first post so I will just post my new WildcardCompare.h. If anyone else finds something it doesn't work for or isn't right let me know so i can try and fix it. Thanks.
test cases
"*this*my*\\**da?.php"
against
"this is my *good day.php"
outcome
true
"???.txt"
against
"bad.txt"
outcome
true
"\\*.txt"
against
"8.txt"
outcome
true
and lastly
"*.php"
against
"MyScript.php"
outcome
true
One problem with this code is that it can't match a literal '?'. Another is its complexity. I can't even try to understand how it works, let alone debug it.
This is how I would approach globbing:
One problem with this code is that it can't match a literal '?'. Another is its complexity. I can't even try to understand how it works, let alone debug it.
This is how I would approach globbing:
Very interesting code.
I tried it out and with simple things it worked, eg. "Hello.*" will match "Hello.cpp".
What doesn't work is "MyFile*.mat.*" trying to match "MyFileForComputers.mat.php" which obviously should match.
Okay well I believe I have it all sorted out. I added using a ? as a single letter wildcard and it appears to work just fine. I wound up having to write another function that compared 2 strings if one had a ? in it. I just went through the string that had the ?'s in it and where ever there was a ? i replaced it with the letter in the second string at that same spot. The main function has not changed from my first post so I will just post my new WildcardCompare.h. If anyone else finds something it doesn't work for or isn't right let me know so i can try and fix it. Thanks.
test cases
"*this*my*\\**da?.php"
against
"this is my *good day.php"
outcome
true
"???.txt"
against
"bad.txt"
outcome
true
"\\*.txt"
against
"8.txt"
outcome
true
and lastly
"*.php"
against
"MyScript.php"
outcome
true
Indeed this code is a bit complex.
It seems to work in many cases, I did find one that doesn't work though.
Let me try to reproduce it with a test string.Edit: "D?or*.material" against "Door424.material" returns false.
@ Zoon - The first example you posted with "MyFile*.mat.*" against "MyFileForComputers.mat.php" worked fine with the code I have. The second example you posted with "D?or*.material" against "Door424.material" did return false and it was a variable name error. On line 40 of my second code post it should be
@ nezachem - I understand it might look a little complex but it is pretty self explanatory. I went the STL route and that sometimes make it look a little more complicated. Also I wanted to try and solve this without using recursion.
I will work on adding support for having a ? be a ? as well. Not sure if I'll get to it today though. I'm attaching the updated code this time instead of posting it to save space.
Well I think I have a solution now that will allow the user to \? to have that be an actual ?. I had to change my approach a little bit and added a couple new functions but it works for the test cases I have tried. I'm just attaching the code again.