Jump to content

Regex - Visual studio 2010 >


Go to solution Solved by Mat,

Recommended Posts

Posted

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.

Posted (edited)

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
  • 3 weeks later...
Posted

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.

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...