سؤال

Let's look at the following illustrative example.

set(TEXT "ab,cc,df,gg")
string(REGEX MATCHALL "((.)\\2)" RESULT "${TEXT}")
message("Result: ${RESULT}")  

# Expected:  Result: cc;gg
# Actual:    Result:

Compare the expected result on regex101.

Does anyone know how to retrieve match group 1 correctly in the above example? Is this possible at all with CMake?

I couldn't find much on the limitations of the regular expression processor used by CMake in the web. Who knows more? (There's a little something written about this in CMake FAQ)

Thanks for the support!

هل كانت مفيدة؟

المحلول

CMake's regular expressions are relatively limited. Look at the static char* regatom (int *flagp) method in RegularExpression.cxx. A \\ indicates that the next character is escaped (treated literally). It looks like there are no back-references possible in the CMake regex.

As a work around, you can invoke shell commands using execute_process.

set(TEXT "ab,cc,df,gg")
message("TEXT: ${TEXT}")

execute_process(
    COMMAND echo ${TEXT}
    COMMAND sed "s/.*\\(\\(.\\)\\2\\).*/\\1/g"
    OUTPUT_VARIABLE RESULT OUTPUT_STRIP_TRAILING_WHITESPACE
    )

message("RESULT: ${RESULT}")  

This produces:

TEXT: ab,cc,df,gg
RESULT: gg

You will have to adjust your regex do produce cc;gg from the given string.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top