0

I'm trying to use c++ std::regex to validate a URL. Using an abbreviated form of John Gruber's web URL matching regex documented here (I just shortened the list of TLDs it will accept), std::regex() throws an exception with code 0x0e (which I believe is a syntax error)

std::regex myRegEx(R"((?i)\b((?:https?:(?:/{1,3}|[a-z0-9%])|[a-z0-9.\-]+[.](?:com|net|org|edu|gov|mil)\b/?(?!@))))")

Note I'm using a c++11 raw string to avoid the need to escape the backslashes.

I'm guessing the c++ regex engine expects a different syntax than what John's expression uses, but I don't know enough to figure out how to adapt it.

What can I do to make this regular expression work with std::regex?

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
Jeff Loughlin
  • 4,134
  • 2
  • 30
  • 47
  • Hello. It should the inline modifier `(?i)`. Please see the marked question. If it didn't solve your problem then edit accordingly. – revo May 17 '19 at 14:38
  • @revo: That was it. Move your comment to an answer and I'll mark it as accepted. – Jeff Loughlin May 17 '19 at 14:44
  • Also, I disagree that this should be closed as a duplicate because it's a different question and might help others trying to use the Gruber expression in c++ (the other answer would not turn up in a search unless you already knew what the problem was) – Jeff Loughlin May 17 '19 at 15:22
  • It's a matter of engine. `std::regex` uses ECMAScript which doesn't support inline modifiers. So if someone doesn't know the syntax and engine that `std::regex` uses it doesn't make sense for them. For this reason I added another dupe to the list. Also perhaps if we just leave "John Grubber" in body and remove it from title and modify title in a way that it clearly asks for a syntax problem it would be better. – revo May 17 '19 at 15:52

0 Answers0