2

I'm working on a project that requires me to find all theorems in LaTeX source, but I'm having trouble with std::regex:

#include <iostream>
#include <regex>

int main() {
  std::regex r("\\begin{theorem}"); // throws `std::regex_error`

  return 0;
}

I've tried constructing this object with different regex_constants, but it always throws exceptions.

Doing a search on SO shows that there were problems with libstdc++, but they were all from several years ago.

I'm using gcc 5.2.0 from homebrew

Why doesn't this work?

6
  • Have you put in a try/catch and printed the message on the exception? Commented Oct 25, 2015 at 22:41
  • When I catch it, the output of e.what() is just regex_error. Commented Oct 25, 2015 at 22:44
  • 1
    Good ole g++. I suspect you need to escape the "{" and "}" as it's probaly wanting to treat those as meta characters and expecting a number in there instead of text. Commented Oct 25, 2015 at 22:47
  • @MichaelAlbers Escaping the curlies seems to have worked. If you add it as an answer, I'll accept it. Commented Oct 25, 2015 at 22:50
  • { is a metacharacter, the regex engine expects a count or a range after it, like {3}, {3,} or {3,6} for instance Commented Oct 25, 2015 at 22:51

1 Answer 1

2

You need to escape the { and }. They are being treated as meta characters as in "a{1,3}".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.