Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expressions (Pattern Matching)

Status
Not open for further replies.

areric

Programmer
Jul 13, 2003
47
0
0
US
Hey all,

Im trying to do a C++ program that checks a string to match a pattern. Namely im looking for it to record the string if it matches
AA###### where A is any alphabetic char (a-z, A-Z) and # os any number 0-9.

I know i can do this in regular expressions as a web based front end im using for this program has it already built in, but i cant figure out how to call ereg() in c++.

If you know of an answer or another alternative to check this pattern its greatly appreciated

Thanks

P.S. im using VS6 not .net
 
i'm not sure it is what you are looking for,but you might try something like this:
Code:
#include <ctype.h>
.....
..
bool checkString( char *str )
{
   if( !isalpha(str[0]) || !isalpha(str[1]))
   { 
        return false;
   }
   for( int i = 2; str[i] != 0; i++ )
   {
      if(!isdigit(str[i])) 
         return false;
   }
   return true;
}
 
thanks alot everyone, ill try out regex++ and see what its like. I ported that function above to string class strings and it works pretty well so thanks.
 
by the way, there is an implementation of regular expressions in ATL. Try somethink like this:
Code:
#include <atlrx.h>
#include<stdio.h>
int main(int argc, char* argv[])
{
   CAtlRegExp<> reUrl;
   // five match groups: scheme, authority, path, query, fragment
   REParseError status = reUrl.Parse("({[^:/?#]+}:)?(//{[^/?#]*})?{[^?#]*}(?{[^#]*})?(#{.*})?" );
   if (REPARSE_ERROR_OK != status)return 0;//unepected error
   CAtlREMatchContext<> mcUrl;
   if (!reUrl.Match("[URL unfurl="true"]http://search.microsoft.com/us/Search.asp?qu=atl&boolean=ALL#results",[/URL] &mcUrl))
   {
      // Unexpected error.
      return 0;
   }
   for (UINT nGroupIndex = 0; nGroupIndex < mcUrl.m_uNumGroups; ++nGroupIndex)
   {
      const CAtlREMatchContext<>::RECHAR* szStart = 0;
      const CAtlREMatchContext<>::RECHAR* szEnd = 0;
      mcUrl.GetMatch(nGroupIndex, &szStart, &szEnd);
      ptrdiff_t nLength = szEnd - szStart;
      printf("%d: \"%.*s\"\n", nGroupIndex, nLength, szStart);
   }
}

Ion Filipski
1c.bmp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top