1

I need to separate type and arguments in this expression:

tf->get_TextState()->set_TextMatrix(std::shared_ptr<DOM::Matrix>(new DOM::Matrix(m->get_M11(), -m->get_M12())));

I create the following regex:

std::shared_ptr[ \t]*<(?'type'[A-Z,:,_,a-z,<,>]*){1}>[ \t]*\([ \t]*new[ \t]*(\k'type'){1}(?'args'.*)

The result is:

Group "type": DOM::Matrix

Group "args": (m->get_M11(), -m->get_M12())));

The problem in a "args" group because I need only the: (m->get_M11(), -m->get_M12())

For do that, I change .* to: \([^()]*+(?:(?R)[^()]*+)*+\)

This expression separetly very well work on: (m->get_M11(), -m->get_M12()))); With expected result: (m->get_M11(), -m->get_M12())

But when I add it in whole pattern i.e.:

std::shared_ptr[ \t]*<(?'type'[A-Z,:,_,a-z,<,>]*){1}>[ \t]*\([ \t]*new[ \t]*(\k'type'){1}(?'args'\([^()]*+(?:(?R)[^()]*+)*+\))

It do not matching anything. What I'm doing wrong?

2
  • 2
    What language is this? Please tag your question with it. Commented Mar 23, 2016 at 22:11
  • What is the output you're looking for (and what is the *+ supposed to do? Commented Mar 23, 2016 at 22:14

1 Answer 1

1

You just need to use a subroutine call to recurse the args subpattern with (?&args), but not the whole pattern ((?R) recurses the whole pattern!).

Use

std::shared_ptr\h*<(?'type'[A-Z:_a-z<>]*)>\h*\(\h*new\h*\k'type'(?'args'\([^()]*+(?:(?&args)[^()]*+)*+\))

See the regex demo

Note that I replaced [ \t] with \h (it matches horizontal whitespace) to shorten the pattern, removed redundant {1} quantifiers and groups.

BTW, here is an alternative expression that does the same thing:

std::shared_ptr\h*<(?'type'[A-Z:_a-z<>]*)>\h*\(\h*new\h*\k'type'(?'args'\((?>[^()]|(?&args))*\))

See another demo

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

1 Comment

Thank you Wiktor! Unfortunately I cant' set the +1 but in my case I estimate this answer +10

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.