Reputation: 11
I was trying to parse a lengthy string through a regular expression, i tried doing it with the following RE and text mentioned at this link http://regexr.com/3a7uf
But when I try to parse the text in c++ using the same RE, the compile time warnings and output not as expected.
Please advise how to get this RE sorted into a format so that it becomes possible to parse text, in a C++ program.
The code goes like this:
std::string str = "root 21015 0.0 0.0 0 0 ? S "
"16:07 0:00 [kworker/5:0]\n root 21095 0.0 0.0 "
" 0 0 ? S 16:08 0:00 [kworker/2:2]\n "
"root 21097 0.0 0.0 0 0 ? S 16:08 0:00 ["
"kworker/u16:4]\n ashish 21103 17.1 1.2 591536 106056"
" ? Sl 16:12 0:01 /usr/lib/firefox/firefox";
std::regex firefox ("[0-9].\..*.firefox");
std::smatch sm;
std::regex_match (str, sm, firefox);
std::cout << "number of matches: " << sm.size() << std::endl;
std::cout << "matches were: " << std::endl;
for (unsigned int i = 0; i < sm.size(); i++)
{
std::cout << "[" << sm[i] << "]";
}
warning during compilation as follows:
warning: unknown escape sequence: '\\.'
regex firefox ("[0-9].\\..*.firefox");
output is as follows:
number of matches: 0
matches were:
Upvotes: 1
Views: 104
Reputation: 11
Thanks for the replies. There was nothing wrong with the expression itself.
I just used the following syntax to create the regular expression and it worked well.
std::regex otherProcessRegEx ("[0-9][0-9][:.:].*.[a-z].|[0-9][:.:].*.[a-z]",
std::regex_constants::ECMAScript |
std::regex_constants::icase);
with c++ 11 were introduced these http://www.johndcook.com/blog/cpp_regex/ different types of RegEx, which were needed to be specified.
:)
Upvotes: 0
Reputation: 1990
This looks to me to just be an issue with the way strings are dealt with. You can try
std::regex firefox (@"[0-9].\..*.firefox");
To indicate that it is a string literal, or if that syntax isn't supported try
std::regex firefox ("[0-9].\\..*.firefox");
Which is saying you really want a \
character in the string, not an escaped period.
From the comment below, it appears as though the C# syntax was not correct, or the periods were meant to concatenate (like PHP?), however they do not concatenate in regular expressions, they are placeholders.
std::regex firefox ("[0-9]+[.][0-9]+[^.]*firefox");
Could you highlight what you want to match, exactly (start to finish), in the sample above? I can't really tell where you want the matching to start, but if you're trying to find numbers and periods, the above would start at the 1.2
.
Upvotes: 1
Reputation: 8551
You're going to have to escape your backslashes for them to be valid C++ strings. For instance, try:
std::regex firefox ("[0-9].\\..*.firefox");
Upvotes: 1