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 1

Status
Not open for further replies.

ADoozer

Programmer
Dec 15, 2002
3,487
0
0
AU
is there a decent regular expression library/dll available in VC+?

If somethings hard to do, its not worth doing - Homer Simpson
 
NVM, msdn seem to be backing the Boost RE libraries.

will give them a try

If somethings hard to do, its not worth doing - Homer Simpson
 
ATL regexp also is good

Ion Filipski
1c.bmp
 
there's also the PCRE project - Perl Compatible Regular Expressions, it's in a nice DLL form.
 
boost regex is already perl compatible. So there are options what you can set to boost to be perl compatible, or not.

Ion Filipski
1c.bmp
 
you know.. i asked this question before .. and i got all sorts of strange answers.. basically saying.. no, but it would be nice..

if you use any of these mentioned here, then please let us know how good they are!
 
drewdaman, boost regex is pretty good, easy to use and performant

Ion Filipski
1c.bmp
 
i downloaded boost, but ive not got round to using it yet, been busy on other projects.

If somethings hard to do, its not worth doing - Homer Simpson
 
I use PCRE with a C++ wrapper around it that makes it much friendlier.

Having used perl regex and gotten accustomed to its the ease of use, it is hard to use straight 'C' versions. I looked at boost early on and found it cumbersome as well.

Keep this thread alive. I will post a link to the VC++ wrapper for pcre this weekend.

It has an interface similar to perl (match, exec, subst) It also somewhat follows the javascript RegEx interface so if you have used that you will be comfortable with the C++ version.
 
I have posted MSVC++ projects and source, docs, and release binaries for RegEx wrapper dll and small executable in which you can test your expressions.

In general the methods return an STL std::pair
Code:
typedef std::pair<bool, std::vector<CString> > REGEX_RET;
// test the return
REGEX_RET reRet = regexObj.exec( testString, RegExPattern, flags );
if( false == reRet.first ) {
  // compile likely failed, test reRet.second.size()
  // should be non zero
  // if compile failed reRet.second[0] will have detailed explanation

visit
feedback welcome
regex questions welcome

You can post here but please send email direct to author for faster response.

Note: the /m (multiline flag) does not recognize \r\n only \n, so if you want your searches to span lines you want to preprocess and convert. the subst method could do that for you.
 
>NullTerminator
Your star :)
Ok, I will put the boost ones


Ion Filipski
1c.bmp
 
this is boost regex, by default it is PCRE, but you can set different flags, described on the technical documentation

match
Code:
//////matching//////////
   char* s = "hello world and everyone else}";
   if(regex_match(s, regex("([^ ]+) (([^ ]*) )*(.*)") )
   {
      ...matches...
   }
extract
Code:
/////extracting///////////
   char* s = "{hello-world-and-everyone-else}";
   char* re = "([^-]+)-([^-]*)-(.*)";
   //#         $1      $2       $3
   cmatch what;
   if(regex_match(s, what, regex(re)))
     for(int i = 0; i < what.size(); i++)
       cout<< "  "<< i<< (i ? " <" : " whole string: <")
           << what[i].str()<< ">"<< endl;
more complicated replace
Code:
/////replacing//////////
   char* s;
   char* re;
   char* fmt;//format
   string out;
   s = "hello to everyone zzzz";
   re = "([h][^\\h]+)|(yyyyy)|(zzzz)";
   cout<< "+-----------BOE--------------"<< endl;
   fmt = "{\\$1=$1;\\$2=$2;\\$3=$3}\n"
         "(?1condition 1 with [$&]\n)"
         "(?2condition 2 with [$&]\n)"
         "(?3condition 3 with [$&]\n)";
   out = regex_replace(string(s), regex(re), fmt, match_default | format_all);
   cout<< out;
   cout<< "+-----------EOE--------------"<< endl;
just replace
Code:
   re = "(hello) (to) (everyone) (z+)";
   fmt = "{$1}{$2}{$3}";
   out = regex_replace(string(s), regex(re), fmt, match_default | format_all);
   cout<< out<< endl;


Ion Filipski
1c.bmp
 
IonFilipski

Thanks for the boost samples - they seem to have greater replacement flexibility than other libraries I've seen.

\0
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top