I am currently using Boost Regex library and am trying to get a function called arguments in C++. For instance, I have a page with HTML and there a JavaScript function called, we will call it something like
XsrfToken.setToken('54sffds');
What I currently have, which isn't working.
std::string response = request->getResponse();
boost::regex expression;
if (type == "CSRF") {
expression = {"XsrfToken.setToken\('(.*?)'\)"};
}
boost::smatch results;
if (boost::regex_search(response, results, expression)) {
std::cout << results[0] << " TOKEN" << std::endl;
}
Where response is the HTML web page, and expression is the regex. The conditional statement is running, therefore I think something is wrong with my regex, but I do not know.
[EDITED]
Forgot to mention that that regex was extracted from PHP and works in a PHP regex checker/debugger
Your mistake not in a regex syntax though the ? is redundant after *, but in C++ string constant literal: the backslash char should be escaped with backslash:
#include <boost/regex.hpp>
#include <iostream>
#include <string>
std::string response("XsrfToken.setToken('ABC')");
boost::regex expression("XsrfToken.setToken\\('(.*?)'\\)");
int main() {
boost::smatch results;
if (boost::regex_search(response, results, expression)) {
std::cout << results[0] << " TOKEN" << std::endl;
}
}
Related
#include <iostream>
#include <iterator>
#include <regex>
#include <string>
std::string ty(std::string text){
if(text == "brown")
return "true";
else
return "qw";
}
int main()
{
std::string text = "Quick $brown fox";
std::cout << '\n' << std::regex_replace(text, std::regex(R"(\\$(.*))"), ty("$&")) << '\n';
}
i use c++11 . I try without if worked but with if don't work ? i don't know what to do
There's a lot of different things wrong with the original code.
Firstly here's some working code
#include <iostream>
#include <iterator>
#include <regex>
#include <string>
std::string ty(std::string text){
if(text == "brown")
return "true";
else
return "qw";
}
int main()
{
std::string text = "Quick $brown fox";
std::smatch m;
if (std::regex_search(text, m, std::regex(R"(\$([[:alpha:]][[:alnum:]]*))")))
{
std::cout << '\n' << ty(std::string(m[1].first, m[1].second)) << '\n';
}
else
{
std::cout << "\nno match\n";
}
}
Some things that were wrong with the original code
Firstly the function being called was wrong. Use std::regex_search to search for matches in a string. Capture the results in an std::smatch object and then use those results to call the ty function.
The regex was wrong in two different ways. Firstly \\ is wrong because you are using a raw string literal, so only a single backslash is required. Secondly (.*) is wrong because that will match the entire rest of the string. You only want to match the word following the dollar. I've used ([[:alpha:]][[:alnum:]]*) instead. That might not be exactly what you want but it works for this example. You can modify it if you want.
My regex is supposed to capture the names of all function declarations:
([\w{1}][\w_]+)(?=\(.+{)
In JavaScript it works as expected:
'int main() {\r\nfunctionCall();\r\nfunctionDeclaration() {}\r\n}'.match(/([\w{1}][\w_]+)(?=\(.+{)/g);
// [ 'main', 'functionDeclaration' ]
In C++ Builder I get this error:
regex_error(error_badrepeat): One of *?+{ was not preceded by a valid
regular expression.
Minimal Reproducible Example:
#include <iostream>
#include <regex>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<string> matches;
string text = "int main() {\r\nfunctionCall();\r\nfunctionDeclaration() {}\r\n}";
try {
//regex myRegex("([\\w{1}][\\w_]+)(?=\\()"); works as intended
regex myRegex("([\\w{1}][\\w_]+)(?=\\(.+{)"); // throws error
sregex_iterator next(text.begin(), text.end(), myRegex);
sregex_iterator end;
while (next != end) {
smatch match = *next;
cout << match.str() << endl;
next++;
}
} catch (regex_error &e) {
cout << "([\\w{1}][\\w_]+)(?=\\(.+{)"
<< "\n"
<< e.what() << endl;
}
}
I used g++ to compile the above, instead of C++ Builder, and the error it gives is different: Unexpected character in brace expression.
The correct regex string literal for C++ is this:
"([\\w{1}][\\w_]+)(?=\\(.+\\{)"
The { must be escaped, unlike in JavaScript.
Hello this is my string
last_name, first_name
bjorge, philip
kardashian, kim
mercury, freddie
in php i am using preg_match_all (pcre) to start regex process
preg_match_all("/(.*), (.*)/", $input_lines, $output_array);
now i installed pcre on c++ and i want to know what exactly process in c++ pcre that equal my php code? what exactly function in c++ pcre that work like php preg_match_all ?
In C++ 11 regular expressions are supported by standard library. So you don't need to use pcre without any specific reasons.
As for example above, you can achieve the same using standard regular expressions. E.g.:
#include <iostream>
#include <string>
#include <regex>
int main()
{
std::vector<std::string> input = {
"last_name, first_name",
"bjorge, philip",
"kardashian, kim",
"mercury, freddie"
};
std::regex re("(.*), (.*)");
std::smatch pieces;
for (const std::string &s : input) {
if (std::regex_match(s, pieces, re)) {
std::cout << "Pieces: " << pieces.size() << std::endl;
for (size_t i = 0; i < pieces.size(); ++i) {
std::cout << pieces[i].str() << std::endl;
}
}
}
return 0;
}
I have these variables:
boost::regex re //regular expression to use
std::string stringToChange //replace this string
std::string newValue //new value that is going to replace the stringToChange depending on the regex.
I only want to replace the first occurrence of it only.
Thanks fellas.
EDIT: I've found this:
boost::regex_replace(stringToChange, re, boost::format_first_only);
but it says the function does not exists, I'm guessing the parameters are incorrect at the moment.
Here is an example of basic usage:
#include <iostream>
#include <string>
#include <boost/regex.hpp>
int main(){
std::string str = "hellooooooooo";
std::string newtext = "o Bob";
boost::regex re("ooooooooo");
std::cout << str << std::endl;
std::string result = boost::regex_replace(str, re, newtext);
std::cout << result << std::endl;
}
Output
hellooooooooo
hello Bob
Make sure you are including <boost/regex.hpp> and have linked to the boost_regex library.
In C++, I'd like to something similar to:
Split on substring
However, I'd like to specify more than one substring to split on. For example, I'd like to split on "+", "foo", "ba" for the string "fda+hifoolkjba4" into a vector of "fda", "hi", "lkj", "4". Any suggestions? Preferably within STL and Boost (I'd rather not have to incorporate the Qt framework or additional libraries if I can avoid it).
I would go with regular expressions, either from <regex> or <boost/regex.hpp>; the regular expression you need would be something like (.*?)(\+|foo|ba) (plus the final token).
Here's a cut-down example using Boost:
std::string str(argv[1]);
boost::regex r("(.*?)(\\+|foo|ba)");
boost::sregex_iterator rt(str.begin(), str.end(), r), rend;
std::string final;
for ( ; rt != rend; ++rt)
{
std::cout << (*rt)[1] << std::endl;
final = rt->suffix();
}
std::cout << final << std::endl;
I suggest using regular expression support in boost. See here for an example.
here is a sample code that can split the string:
#include <iostream>
#include <boost/regex.hpp>
using namespace std;
int main()
{
boost::regex re("(\\+|foo|ba)");
std::string s("fda+hifoolkjba4");
boost::sregex_token_iterator i(s.begin(), s.end(), re, -1);
boost::sregex_token_iterator j;
while (i != j) {
std::cout << *i++ << " ";
}
std::cout << std::endl;
return 0;
}