0

I have string like this one:

    parser = new ASTParser(in, encoding);

After i tokenize those string I get:

    ASTParser(in, encoding);

Now, how can I perform regex to get only ASTParser?

I tried to do this using std::erase:

    str.erase(std::remove_if(str.begin(), str.end(), isLetter), str.end());

but the problem is that I get also parameters (ASTParserinencoding) and I don't want this.

Also, I would very appreciate solution with boost::regex.

chao
  • 1,893
  • 2
  • 23
  • 27
  • You don't need a regex for this, you can simply stop at the ( – mah Jun 09 '13 at 20:39
  • this is also a solution, but there can be more situations like: `ASTParser (in, encoding)`, `ASTParser;`. So, it is OK if I just stop if I find space, semi-colon, etc. or is some better generic solution for this? – chao Jun 09 '13 at 20:41
  • 1
    I don't know how to use boost::regex, but if you have `ASTParser(in, encoding);` in a string, the regex `([^\(]+)` should be able to capture it, by capturing everything from the first character until `(` is seen. – mah Jun 09 '13 at 20:44

1 Answers1

2

No need for a regex -- all you have to do is scan until the first non-letter. Here's one way (note my std::string manipulation skills are a little rusty):

str.substr(0, std::distance(str.begin(), std::find_if_not(str.begin(), str.end(), isLetter));

Alternatively, you could use a regex like this:

([A-Za-z_][A-Za-z0-9_]*)
Cameron
  • 96,106
  • 25
  • 196
  • 225