Jump to content
Sign in to follow this  
JohnOne

Regex - Visual studio 2010 >

Recommended Posts

JohnOne

VS 2010 has std::tr1::regex which resides in <regex>

I was hoping this standard support would be similar to AutoIt3 stringregex, but I'm beginning to think otherwise now, but I'm hoping I'm wrong.

Has anyone figured a way (if there is one) to get an array of all matches of a pattern with one call to regex_search() with a flag of some kind, or are multiple calls and iteration absolutely necessary?


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
Mat

More accurately the C++11 standard includes a regex library.

Have you looked at sregex_iterator?? Should mean only one call and then a for loop, but I have never actually used that library.

Edited by Mat
  • Like 1

Share this post


Link to post
Share on other sites
JohnOne

I never new it was C++ 11, never thought VS2010 had that.

Anyway, you are correct about std::sregex_iterator.

I suppose it was just wishful thinking to hope for something as ace as StrngRegExp().


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
danielkza

An example for you (tested only on VS2012). It's not particularly complicated if you know C++ already, just a bit verbose, but that's usually the price for the good stuff in C++.

#include <string>
#include <iostream>
#include <regex>


int main(int argc, char **argv)
{
    std::string data("a.b.c.d");
    std::regex pattern("([^.])+(.|$)");
    std::regex_iterator<std::string::iterator> it(data.begin(), data.end(),
        pattern);

    for(; it != decltype(it)(); ++it)
    {
        auto match = *it;
        if(!match.empty())
            std::cout << match[1].str() << std::endl;
    }

    return 0;
}
Edited by danielkza
  • Like 1

Share this post


Link to post
Share on other sites
Ascend4nt

If you are using Windows XP+, or Win 2K w/IE 5.5, you can skip C++ regular expressions and use the Visual Basic RegExp library which is on every system post WinXP. There's a few articles about using it, but I decided to write my own C/C++ version. Here's two places you can look:

http://www.codeproject.com/Articles/4594/Use-regular-expression-in-your-C-program

http://www.codeproject.com/Articles/6274/Convenient-wrapper-of-VBScript-RegExp-for-VC

Note that the VB RegExp library doesn't allow lookbehind assertions, just as Visual C++ doesn't. I'd instead recommend the same library that AutoIt uses, PCRE (http://www.pcre.org/).

And just in case someone complains, I'll state the obvious here: the VB RegExp library is not portable to other O/S's, of course.

  • Like 1

Share this post


Link to post
Share on other sites
Richard Robertson

http://www.pcre.org/

This is what AutoIt uses. I should have posted this before but had forgotten.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×