Are there any 'Like'-string-compare function?

Started by
14 comments, last by DerekSaw 21 years, 9 months ago
Well, in VB, you can have IF (strName Like "John*") THEN ... or in SQL, ... WHERE name LIKE ''John%'' ... how about in C++? Do the libraries given by MSVC have these LIKE* string comparison?
"after many years of singularity, i'm still searching on the event horizon"
Advertisement
Are you talking about regular expressions? It''s not in the
standard, but you can get regex libraries like Boost.


~~~~
Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!
I'm thinking if I could do this (yes I know I could write one function for it, but I hope there's some simple come-with-the-compiler function):
if (strlike(name, "John*")){  // anyone with the name begin with 'John'}  


[EDIT] Wow! boost:regex sure fantastic. But I don't need such powerful pattern matching. Guess I need to write one simple function that recognizes '*' and '?'. Thanx.

[edited by - DerekSaw on July 15, 2002 11:54:54 PM]
"after many years of singularity, i'm still searching on the event horizon"
You mean you only want to check the first n characters for equality? strncmp , or strnicmp if you're the case insensitive type

EDIT: Oh, if you want the wildcard functionality, I don't know of any standard functions for that

[edited by - Zipster on July 15, 2002 12:27:44 AM]
The POSIX standard has a ''regcomp'' function included, if you have access to POSIX libraries. Otherwise, you probably want to go with the Boost suggestion above (Boost includes a lot of nice things that you won''t find in the ANSI/ISO standards).


          //if your using C++ you can use the std::string function members to search the string for a patten match, being the first name..Find will search the entire string and return the position in the string if match is found.#include <string>using namespace std;main(){    string str;    int id1 = -1;    id1 = str.find("JOHN",0);    // or    id1 = str.find_first_of("JOHN",0);    if (id1 != -1) {        // Found the string    }    // Else none found....}  


I use the string.find a lot for pattern matching, it's nice and fast.

str.find("PATTERN TO MATCH",int POSTION TO START SEACHING);

There also a lot of other things you can use,, also string.substr() to cut out the string into another, and string.erase() to remove certain parts..

[edited by - tonic151 on July 16, 2002 10:14:47 AM]
Depends on exactly what pattern-matching you need. If you are just checking substrings, then you can use the substr() member function of std::string. You might also like to take a look at the other members of std::string. If you need fairly complex pattern-matching, then you probably need a regex library such as the Boost one already mentioned.
Although a simple pattern matching function shouldn''t be too hard to create from scratch, assuming you only need simple wildcards such as ''*'' and ''?''. A combination of strstr() (old fashioned C baby!)... um, actually all you would need is a substring checker, and then check offets in the string, like if it''s at the beginning, end, or if you are using the ''?'' wildcard which defines an explicit number of characters that are "wild". Who knows, you might have fun! *gasp* No, not fun writing a pattern matching function!
^[a-zA-Z0-9]{0,8}\.?[a-zA-Z0-9]{0,3}$

gotta love regular expressions.

/me waits for someone to figure out what that one is and correct him because he didn''t make it permissive enough

*wanders off*
Shouldn''t DOS filenames be all uppercase?

"The word and works of God is quite clear, that women were made either to be wives or prostitutes."
Martin Luther
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]

This topic is closed to new replies.

Advertisement