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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With