• Advertisement

Archived

This topic is now archived and is closed to further replies.

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

This topic is 5761 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

          

//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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
^[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*

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement