I need a function to identify a pattern string within another string while taking into consideration a wildcard character _
For instance, if the given pattern is f__h, then it should match up to the string catfish.
So essentially, the underscore needs to be able to represent any alphabetical character, but I can't think of a way to go about this. Anyone got some ideas on how to do this?
Thanks.
With new standard C++11 you can use regex. Otherwise use boost regex.
What's wrong with:
std::search(
text.begin(), text.end(),
pattern.begin(), pattern.end(),
[]( char fromText, char fromPattern ) {
return fromPattern == '_' || fromPattern == fromText;
} )
If the function returns text.end(), there was no match.
Otherwise, it returns an iterator to the first character of the
match.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With