If you have a string of text:
string SampleText = "12345 quick 12345 brown 12345 fox";
and you wanted to use a regular expression to identify the words in the SampleText (a word is defined as a group of letters not separated by a space), what is the c++ code that would do it? What #include statements would I need?
Complete code would be helpful, but any help is appreciated.
Using regular expressions with C++?
If you are using unix:
you can use %26lt;regex.h%26gt;
Posix function are: regcomp, ergexec, and regerror.
you can type 'man regcomp' to get an explanation of how to use the functions.
If you are using Windows, here is some sample code:
// regex_parse.cpp
// compile with: /clr
#using %26lt;system.dll%26gt;
using namespace System;
using namespace System::Text::RegularExpressions;
int main( )
{
int words = 0;
String^ pattern = "[a-zA-Z]*";
Console::WriteLine( "pattern : '{0}'", pattern );
Regex^ regex = gcnew Regex( pattern );
String^ line = "one\ttwo three:four,five six seven";
Console::WriteLine( "text : '{0}'", line );
for( Match^ match = regex-%26gt;Match( line );
match-%26gt;Success; match = match-%26gt;NextMatch( ) )
{
if( match-%26gt;Value-%26gt;Length %26gt; 0 )
{
words++;
Console::WriteLine( "{0}", match-%26gt;Value );
}
}
Console::WriteLine( "Number of Words : {0}", words );
return 0;
}
-----------------------
Note, the using namespace statement up top ends in: RegularExpressions;
The ... is a yahoo answers problem.
sp
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment