Actually code

const char *first = "фывфыв@NameOfElement(pupa i lupa)вфывфыв"; const char *last = first + strlen(first); cmatch mr; regex rx("@NameOfElement\((.*?)\)"); regex_search(first, last, mr, rx); cout << mr.str(); 

The output looks like this: @NameOfElement

As far as I understand regular expressions (and I do it badly), the construction of the form

  (.*?) 

this is the so-called capturing group, which in my regular calendar corresponds to the text inside the annotation brackets, can I somehow get it from the search results?

  • Firstly, there is a problem with screening \ , \( should be \\( and so on - VTT

1 answer 1

You need to use a .str(1) object of type cmatch (or smatch ):

 const char *first = "фывфыв@NameOfElement(pupa i lupa)вфывфыв"; const char *last = first + strlen(first); cmatch mr; regex rx(R"(@NameOfElement\((.*?)\))"); regex_search(first, last, mr, rx); cout << mr.str(1); // => pupa i lupa 

See the demo online .

I used raw string literal , R"(@NameOfElement\((.*?)\))" , This is equivalent to "@NameOfElement\\((.*?)\\)" . By the way, it is better to replace .*? on [^)]* or [^()]* (= zero and more characters other than ) / ( ).