How to match a sequence of whitespaces with c++11 regex - regex

std::string str = "ahw \t\n";
std::regex re(R"((\s)*)");
std::smatch mr;
if (std::regex_search(str, mr, re))
{
std::cout << "match found: " << mr.size() << "\n";
for (size_t i = 0; i < mr.size(); ++i)
{
std::string strrep = mr.str(i);
int len = mr.length(i);
std::cout << "index: " << i << "len : " << len << " string: '" << strrep << "'\n";
}
}
std::string newStr = std::regex_replace(str, re, "");
std::cout << "new string: '" << newStr << "'\n";
result:
What I expect: only 1 match, strrep should be ' \t\n', and len should be len(strrep) = 6. But both vc12 and gcc4.9.2 show the above result.
What's wrong with my understand? How could I match the whitespace sequence ' \t\n'?

Just turn \s* to \s+ in your regex because \s* matches an empty string also(ie, \s* matches zero or more spaces) also and you don't need to have a capturing group.

Related

How to match the following coordinates pattern using regex on C++?

I'd like to check if it match the following format:
(integer,integer), including the parenthesis and the commas. For example:for (3,4) would return true and for (6.4 would return false
I tried with
string input;
regex check("(\\-|+)?[[:d:]]+,?(\\-|+)?[[:d:]]+");
cin >> input;
if (regex_match(input, check)) cout << "okay" << endl;
else cout << "error";
but I'm getting runtime error
It seems you are looking for
regex check(R"(\([-+]?\d+,[-+]?\d+\))")
This defines a pattern like ^\([-+]?\d+,[-+]?\d+\)$ when used with std::regex_match that requires a full string match.
Details:
^ - start of string (implicit in regex_match)
\( - a (
[-+]? - 1 or 0 + or - chars
\d+ - 1 or more digits
, - a comma
[-+]? - 1 or 0 + or - chars
\d+ - 1 or more digits
\) - a )
$ - end of string (implicit in regex_match)
C++ demo:
regex check(R"(\([-+]?\d+,[-+]?\d+\))");
string s1("(44,45)");
string s2("(44,45");
smatch match;
if (regex_match(s1, match, check)) {
cout << s1 << ": Matched!" << endl;
} else {
cout << s1 << ": Not matched!" << endl;
}
if (regex_match(s2, match, check)) {
cout << s2 << ": Matched!" << endl;
} else {
cout << s2 << ": Not matched!" << endl;
}
Output:
(44,45): Matched!
(44,45: Not matched!
Try input this regex \(\d{1,},\d{1,}\)
Maybe it might works

How to use regex_token_iterator<std::string::iterator> get submatch's position of original string by the iterator itself?

Below is the code to find the match of "\b(sub)([^ ]*)" in "this subject has a submarine as a subsequence". But I also want to know the position of those sub matches in original string by regex_token_iterator itself. The result should be 5, 19, 34.
// regex_token_iterator example
#include <iostream>
#include <string>
#include <regex>
int main ()
{
std::string s ("this subject has a submarine as a subsequence");
std::regex e ("\\b(sub)([^ ]*)"); // matches words beginning by "sub"
// default constructor = end-of-sequence:
std::regex_token_iterator<std::string::iterator> rend;
std::cout << "entire matches:";
std::regex_token_iterator<std::string::iterator> a ( s.begin(), s.end(), e );
while (a!=rend) std::cout << " [" << *a++ << "]";
std::cout << std::endl;
return 0;
}
Output:
entire amtches: [subject] [submarine] [subsequence]
*a return a pair of two iterators over the string s. You could try this:
std::cout << " [" << *a++ << ' ' << a->first - s.begin() << "]";
or this
std::cout << " [" << *a++ << ' ' << std::distance(s.begin(), a->first) << "]";

regex_search and substring matching

Here is my code:
std::string var = "(1,2)";
std::smatch match;
std::regex rgx("[0-9]+");
if(std::regex_search(var,match,rgx))
for (size_t i = 0; i < match.size(); ++i)
std::cout << i << ": " << match[i] << '\n';
I want to be able to extract both 1 AND 2, but so far output is just the first match (1). I can't seem to figure out why and my brain is fried. It's probably something obvious
regex_match's elements are for matching groups within the regex.
In a slightly modified example
std::string var = "(11b,2x)";
std::smatch match;
std::regex rgx("([0-9]+)([a-z])");
if(std::regex_search(var,match,rgx))
for (size_t i = 0; i < match.size(); ++i)
std::cout << i << ": " << match[i] << '\n';
You'd get the following output:
0: 11b
1: 11
2: b
What you want is to use std::regex_iterator to go over all the matches:
auto b = std::sregex_iterator(var.cbegin(), var.cend(), rgx);
auto e = std::sregex_iterator();
std::for_each(b, e, [](std::smatch const& m){
cout << "match: " << m.str() << endl;
});
This will yield the desired output:
match: 1
match: 2
live demo

Pcre php regex equal in c++

hello this is pcre regex (php regex)
/\h*(.*?)\h*[=]\h*("(.*?(?:[\\\\]".*?)*)")\h*([,|.*?])/
this regex work for this string
data1 = "value 1", data2 = "value 2", data3 = " data4(" hey ") ",
and get
data, data2, data3
val, val2, data4("val3")
what is this regex equal in c++ regex ?
You should replace \h with \s and use \\ inside a raw string literal.
Refer to the following example code:
#include <string>
#include <iostream>
#include <regex>
using namespace std;
int main() {
std::string pat = R"(\s*(.*?)\s*=\s*(\"(.*?(?:[\\]\".*?)*)\")\s*([,|.*?]))";
std::regex r(pat);
std::cout << pat << "\n";
std::string s = R"(data1 = "value 1", data2 = "value 2", data3 = " data4(" hey ") ",)";
std::cout << s << "\n";
for(std::sregex_iterator i = std::sregex_iterator(s.begin(), s.end(), r);
i != std::sregex_iterator();
++i)
{
std::smatch m = *i;
std::cout << "Capture 1: " << m[1].str() << " at Position " << m.position(1) << '\n';
std::cout << "Capture 3: " << m[3].str() << " at Position " << m.position(3) << '\n';
}
return 0;
}
See IDEONE demo and a JS (ECMA5) regex demo

Exploit regex_search

I am trying to use Boost regular expressions module to extract the numbers from character strings of this format: "{ 12354,21354, 123 }"
The following code has been written to this end. As I do this operation in a loop the string is stored in it->c_str():
boost::cmatch matches;
boost::regex reNumber("-*[0-9.]+");
boost::regex reFiniteValues(" *\\{.*\\} *");
std::cout << "\ttesting for finite values" << std::endl;
if (boost::regex_match(it->c_str(), matches, reFiniteValues))
{
boost::regex_search(it->c_str(), matches, reNumber);
std::cout << "matches.size(): " << matches.size() << std::endl;
for(unsigned int i = 0; i < matches.size(); ++i)
{
std::cout << matches[i] << std::endl;
}
if (matches.size() > 0)
{
std::cout << "\tpattern found" << std::endl;
continue;
}
}
However the size of the matches object is 1, and it only contains 12354 in this example. I would like to know how I can retrieve all the numbers from the string.
You could maybe try to loop regex_search(). -
typedef std::string::const_iterator SITR;
std::string str = it->c_str();
SITR start = str.begin();
SITR end = str.end();
boost::smatch m;
while ( boost::regex_search (start, end, m, reNumber ) )
{
std::cout << m[0].str() << std::endl;
start = m[0].second;
}