Friday, July 31, 2009

Regular expressions with C++?

If you have a string of text and you wanted to use a regular expression to identify any words in that string of text, what is the c++ code that would do it? What #include statements would I need?





Note: A word is defined as any sequence of letters that is not interrupted by a space.





Complete code would be helpful, but any help is appreciated.

Regular expressions with C++?
I believe from this description, what you are trying to do is identify whether a given word is contained within a character string - in other words, you are looking for a substring.





If so, the function you are looking for is the famous "stir-stir", or





char * strstr ( const char * string1, const char * string2 );





The function is in the string.h library, which you need to include:


#include %26lt;string.h%26gt;





The function returns a null pointer if the substring (string2) is not located in string1. Otherwise it returns a pointer to string2.





This link has an example of use:


http://www.cplusplus.com/ref/cstring/str...





====


Addendum:


I see that I misunderstood your question, but I will let my answer stand for now. If I get the chance I will come back later and show you some code to parse a string for words as you describe.





====


Addendum #2


I completely missed on your question. Sometimes that happens, even with the best of intentions.





C++ does not come "out of the box", any box version, with regular expressions. You have to add an external library, and there are a few out there:


like this:


http://www.onlamp.com/pub/a/onlamp/2006/...


or this:


http://ourworld.compuserve.com/homepages...
Reply:OK answering this one is a bit tough as I worked with regex long time ago. But a simple search in Google will bring you some good results. Here is one place I found very help full just from a quick glance. http://www.tropicsoft.com/Components/Reg...


No comments:

Post a Comment