std::regex_error exception thrown at runtime - c++

Given that this code works:
regex r1{ "fish"s };
smatch m1;
if (regex_search("I love fish and chips"s, m1, r1))
cout << m1[0] << endl;
I believe that VS2015 supports regular expressions. However, initialization of this regular expression object:
regex r{ R"(\d{2,3}(-\d\d) { 2 })" };
throws a std::regex_error exception. What's wrong with the initialization?

So, yeah, as mentioned in the comments:
(\d{2,3}(-\d\d) { 2 })
should be
(\d{2,3}(-\d\d){2})
otherwise the {2} relates to the space instead of the (-\d\d), and other weird things might possibly happen as well…

You have a typo in your regex. Change this:
regex r{ R"(\d{2,3}(-\d\d) { 2 })" };
To:
regex r{ R"(\d{2,3}(-\d\d){2})" };

Related

How to check a specified string is a valid URL or not using C++ code

there any possible way to check that the specified string is a valid url or not. The solution must be in c++ and it should work without internet.
example strings are
good.morning
foo.goo.koo
https://hhhh
hdajdklbcbdhd
8881424.www.hfbn55.co.in/sdfsnhjk
://dgdh24.vom
dfgdfgdf(2001)/.com/sdgsgh
\adiihsdfghnhg.co.inskdhhj
aser//www.gtyuh.co.uk/kdsfgdfgfrgj
Chose a symphatetic regular expression like /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/.
Use std regex, or boost regex if you don't have C++11:
if (std::regex_match ("http://subject", std::regex("^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$") )) {
// ...
}
You could use regex.
What a regex is.
With C++11 the regex are build-in the STD library
regex c++11.
If you cannot use C++11, for some reason, you could use boost library.
Anyway you could check the patter of an url with:
#include <regex> //require c++11
// ...
// regex pattern
std::string pattern = "https?:\/\/(www\.)?[-a-zA-Z0-9#:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9#:%_\+.~#?&//=]*)";
// Construct regex object
std::regex url_regex(pattern);
// An url-string for example
std::string my_url = "http://www.google.com/img.png";
// Check for match
if (std::regex_match(my_url, url_regex) == true) {
std::cout << "This is a well-formed url\n";
} else {
std::cout << "Ill-formed url\n";
}

Use REGEX to replace X(...) with X{...}

The problem comes from updating code to C++11, which uses initialisers.
So:
a = X(4);
b = X();
c = X( (1+2)*(3+4) );
void P : X(5) { foo(); }
Becomes
a = X{4};
b = X{};
c = X{ (1+2)*(3+4) };
void P : X{5} { foo(); }
My IDE (XCode) supports RegEx search and replace.
I've tried:
X\((.*)\) -> X{$1}
But it fails on:
X(foo); Y(bar); -> X{foo); Y(bar};
Is there any way to accomplish this transformation?
EDIT: might this answer hold the key? Or this one?
EDIT: Sorry, my list of examples was incomplete. It's going to be difficult to categorise in advance, so I will just have to keep amending the question until I've got all the edge cases. I think the problem is that any kind of trick cannot be relied upon.
This is because the operators * and + are greedy. However, you can enforce laziness by using the expression
X\((.*?)\);
The ? Symbol enforces a lazy match.

wsregex::compile crashes (memory leak) when handling regex string?

I would like to understand why my program crashes when I try to use the wsregex::compile of BOOST with the following string:
(?P<path>\b[a-z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*)?
(:)?
(?P<ip>(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)
(;(?P<port>\d*))?
(:(?P<port>\b\d+\b):(?P<password>[\w]*))?
(:(?P<password>\b\d+\b))?
In regex buddy everything appears to be fine. I used the JGSoft flavor option on RegexBuddy.
I am validating the following:
c:\My Documents\Test\test.csv:1.12.12.13:111:admin
c:\My Documents\Test\test.csv:1.12.12.13:111
c:\My Documents\Test\test.csv:1.12.12.13;111
1.12.12.13:111
1.12.12.13;111
Can you guys help me. Thanks a lot.
This is neither a memory leak nor a crash as far as I can tell. Xpressive is throwing an exception because this is an invalid pattern. The following program:
#include <iostream>
#include <boost/xpressive/xpressive_dynamic.hpp>
namespace xpr = boost::xpressive;
int main()
{
const char pattern[] =
"(?P<path>\\b[a-z]:\\\\(?:[^\\\\/:*?\"<>|\\r\\n]+\\\\)*[^\\\\/:*?\"<>|\\r\\n]*)?"
"(:)?"
"(?P<ip>(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b)"
"(;(?P<port>\\d*))?"
"(:(?P<port>\\b\\d+\\b):(?P<password>[\\w]*))?"
"(:(?P<password>\\b\\d+\\b))?";
try
{
xpr::sregex rx = xpr::sregex::compile(pattern);
}
catch(xpr::regex_error const & e)
{
std::cout << e.what() << std::endl;
}
}
Outputs:
named mark already exists
Indeed, it does. This pattern uses "port" and "password" twice as the name of a capturing group. Xpressive doesn't like that. Just pick unique names for your captures and you should be fine.

I can't get StringTemplate to indent

I have the following StringTemplate group
group RPInstr;
before(firstStat) ::= <<
<{<[beforeEnteringInstr(),firstStat]; anchor, separator="\n">}; anchor>
>>
beforeEnteringInstr() ::= "before();"
I am trying to make the first statement of a method to align with the instrumentation code ("before();").
However, what I am getting right now is something like this:
public int method() {
before();
System.out.println("testing");
System.out.println("testing again");
}
What is the proper way to indent before(); and the next statement so I get the following?
public int method() {
before();
System.out.println("testing");
System.out.println("testing again");
}
Thanks in advance.
PS: the options that I am using in my grammar are the following
options {output=template; rewrite=true; backtrack=true; memoize=true;}
the expr in before(firstStat) is not indented. No reason for ST to indent then.

D std.regex - return as text

Is there a possibility to return text which was used to create regular expression?
Something like this:
auto r = regex(r"[0-9]", "g"); // create regular expression
writeln(r.dumpAsText()); // this would write: [0-9]
There is nothing in http://dlang.org/phobos/std_regex.html on this. (or at least I did not notice)
No, because it compiles the regex, and I don't believe it even stores the string after compilation.
The best thing to do is just to store the string yourself on creation.
Source for struct Regex
As you can see, it doesn't store the pattern string, only the bytecode.
Typically using a subtype would work, but unfortunately ti doesn't due to failed template constraints. E.g. a plausible solution (that doesn't work right now) would be to wrap the regex as a subtype:
auto myregex(string arg1, string arg2)
{
struct RegexWrap
{
Regex!char reg;
alias reg this;
string dumpAsText;
}
return RegexWrap(regex(arg1, arg2), arg1);
}
void main()
{
auto r = myregex(r"[0-9]", "g"); // create regular expression
writeln(r.dumpAsText); // this would write: [0-9]
writeln(match("12345", r)); // won't work
}
The match function in std.regex won't work with this wrapper struct even when using a subtype, because it fails this template constraint:
public auto match(R, RegEx)(R input, RegEx re)
is(RegEx == Regex!(BasicElementOf!R)
Even if you changed the header to this, it still won't work:
public auto match(R)(R input, Regex!(BasicElementOf!R) re)
The only way it would work is if the type was explicit so the subtype could be passed:
public auto match(R)(R input, Regex!char re)
I find this to be an awkward part of D that could be improved.