Splitting a multiline string into individual line (at least getting the last one)

Hi

I’ve got in some variable MYSTRING a multiline string, for instance:

CMAKE_CXX_FLAGS_DEBUG: -g
CMAKE_CXX_FLAGS_RELWITHDEBINFO: -O2 -g -DNDEBUG
CMAKE_CXX_FLAGS_RELEASE: -O3 -DNDEBUG

I’d like to get only

CMAKE_CXX_FLAGS_RELEASE: -O3 -DNDEBUG

My current approach is

string(REGEX MATCHALL "^.+$" MYSTRINGS ${MYSTRING})
list(GET MYSTRINGS -1 MYSTRING)

Though my regex seems to work (at least I tested on https://regex101.com/), in CMake I got only one match (MYSTRINGS has length 1) and MYSTRING is the original one plus additional line feeds.

Can someone provide the right solution?

Thanks,
A.

RegEx work a bit differently in every programming language, that’s why you can select it in that web service and also the modifiers. In your case, the “multi line” mode was preselected.
That’s the only reason why it works in the web service, but AFAIK CMake has no such mode.

For your use case, you can use "[^\n\r]+" which has the desired effect.

Or if the content comes from a file, it might be better to immediately read if with file(STRINGS) to split it into an array of lines.

Perfect! Thx