Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does C++ regex only find a part of what javascript regex find in a string?

I need to use regex in my code, but it doesn't work as it should. I would like to search for Test.list.everything in a file I read line per line, but it only gets everything. I shortened my code to make my problem clear. In different regex test programs my expression works correctly as you can see it in this test program: http://regex101.com/r/iR1cH1.

The input I used there is

summer1969 2014-03-07 Test.list.everything.1234 123.4.4.34 moreCode

and the regex (([a-zA-Z]+\.{0,1})+)\.[0-9]+.

It doesn't work correctly either if I used std::tr1::regex instead of std::regex. I don't know the difference between this two expressions, is there any?

What is the difference between C++ and the other programs I used? How can I tell C++ regex to behave like other ones?

#include <regex>
#include <iostream>

int main()
{
    std::string output = "summer1969 2014-03-07 Test.list.everything.1234 123.4.4.34 moreCode";
    std::cmatch result;
    std::regex rx ("(([a-zA-Z]+\\.{0,1})+)\\.[0-9]+");
    std::regex_search(output.c_str(), result, rx);
    for(int i = 0; i < result.size(); i++)
    {
        std::cout << i << ". " << result[i] << std::endl;
    }
    system("pause");
    return(0);
}

Output:

0. everything.1234
1. everything
2. everything.
like image 692
Safiye Avatar asked Jan 23 '26 01:01

Safiye


1 Answers

It looks like a bug in Visual C++ 2010. If you want to capture the text followed by dot and number, then you can use the following regex:

"[a-zA-Z\\.]+(?=\\.\\d+)"

It works fine.

like image 140
Ulugbek Umirov Avatar answered Jan 24 '26 20:01

Ulugbek Umirov