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

If you have a string of text

(for example, "1234 quick 1234 brown 1234 fox")

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?

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

I could really use an example of code.

What #include statements would I need? Is there a way to do it without including regex? Where do I get regex (if I try to include it now, my compiler gives me an error, so I assume I need to get it from somewhere)?

Please note, I don't need a tutorial on regular expressions. I understand regular expressions well enough as it is, I just need to know how to use them in C++.

2006-09-28 08:04:42 · 3 answers · asked by Anonymous in Computers & Internet Programming & Design

3 answers

well... one way would be to search through the string until you found a space, or CR/LF.

Im not sure what you want to do when you find them but something like this will count them:

char mystring[] = "1234 quick 1234 brown 1234 fox";
int count, words = 0, spaces = 0, crlf = 0;
BOOL wordsstart = 1;

for (count = 0; count < sizeof(mystring); count++)
{
if (mystring[count] == 0x20) //space
{
spaces++;
wordstart = 1;
}
else if (mystring[count] == 0x0a || mystring[count] == 0x0d) //carriage return or line feed
{
crlf++;
wordstart = 1;
}
else if (wordstart && ((mystring[count] >= 'a' && mystring[count] <= 'z') || (mystring[count] >= 'A' && mystring[count] <= 'Z')))
{
words++;
wordstart = 0;
}
}

its just off the top of my head, but it should work

2006-09-28 09:25:21 · answer #1 · answered by justme 7 · 1 0

you will desire to examine the record character-via-character (probable 'getc'). Have a "line buffer" like char buffer[one hundred] it is lots longer than the reveal length you're employing (longer than 50 for this reason.) As you examine the characters from the record, upload (append) one and all to the buffer, incrementing the index the place you extra it. whilst the character you examine became an area, then examine your buffer index. If it is under 50, superb, proceed. in spite of the undeniable fact that it is 50 or extra desirable, then: run BACKWARDS till you detect a (previous) area bar character. Now THAT area bar would desire to truly be the tip of line, so print the line buffer as much as that component and then a newline (n). and then take the characters on your line buffer previous that (ie, the extra chars that would desire to have run off the line, and pass (replica) them lower back to the commencing up of the line buffer, set your index AFTER them, and proceed on. That way the 'overflow' characters will sort the commencing up of the subsequent line to print. And only proceed on analyzing chars and printing like the above till eof (end of record). certainly, you will desire to do "sanity tests": it is, whilst analyzing chars, do not permit your self bypass previous the tip of that one hundred char line buffer. that would desire to point somebody has entered an prolonged string without areas and it could overflow your reveal. additionally whilst working backwards to locate the previous area bar, do not run under the [0] index of your line buffer. lower back meaning there is an rather long textual content cloth without areas. For that situation (which will desire to be uncommon, yet evaluate how long some URLs get) you will would desire to discern some thing out - probable ok then to interrupt that rather long textual content cloth. besides, it is a few thing you often would desire to do: to examine for unusual circumstances on your enter and look after them. stable luck!

2016-10-18 03:40:01 · answer #2 · answered by Anonymous · 0 0

google up pcre

http://www.pcre.org/!

2006-09-28 08:32:53 · answer #3 · answered by jake cigar™ is retired 7 · 0 1

fedest.com, questions and answers