Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How get string inside bracket "()" using cmake regex

I am trying to get the string inside ‘( )’ by using CMake regex.
here is the example which I tried:

set(STR "example(arg1,arg2)")
string(REGEX MATCH "^\(.*\)$" ARG_STR ${STR}) 

expected: arg1,arg2, but I got example(arg1,arg2)
Please guide me how to fix this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use a capturing group with a pattern lie

string(REGEX MATCH "\\(([^()]*)\\)" _ ${STR}) 

Then, you can access the contents using ${CMAKE_MATCH_1}, e.g.

message("ARGS_STR: ${CMAKE_MATCH_1}")

See CMAKE reference details:

New in version 3.9: All regular expression-related commands, including e.g. if(MATCHES), save subgroup matches in the variables CMAKE_MATCH_ for <n> 0..9.

Also, see the regex demo.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading