English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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.

2006-09-28 05:23:08 · 2 answers · asked by Anonymous in Computers & Internet Programming & Design

2 answers

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

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/strstr.html

====
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/04/06/boostregex.html?CMP=OTC-6YE827253101&ATT=Regular+Expressions+in+C+with+Boost+Regex
or this:
http://ourworld.compuserve.com/homepages/John_Maddock/regexpp.htm

2006-09-28 06:49:28 · answer #1 · answered by TJ 6 · 0 0

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

2006-09-28 06:51:04 · answer #2 · answered by yairs2000 3 · 0 1

fedest.com, questions and answers