如何在CMake中获得正则表达式的组匹配?

时间:2021-01-04 11:25:46

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.

比较regex101上的预期结果。

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

有没有人知道如何在上面的例子中正确检索匹配组1?这有可能与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)

我找不到很多关于CMake在网络中使用的正则表达式处理器的限制。谁知道更多? (在CMake FAQ中有一些关于此的内容)

Thanks for the support!

感谢您的支持!

1 个解决方案

#1


3  

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.

CMake的正则表达式相对有限。查看RegularExpression.cxx中的static char * regatom(int * flagp)方法。 \\表示下一个字符被转义(按字面处理)。看起来CMake正则表达式中没有可能的反向引用。

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

作为解决方法,您可以使用execute_process调用shell命令。

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; gg。

#1


3  

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.

CMake的正则表达式相对有限。查看RegularExpression.cxx中的static char * regatom(int * flagp)方法。 \\表示下一个字符被转义(按字面处理)。看起来CMake正则表达式中没有可能的反向引用。

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

作为解决方法,您可以使用execute_process调用shell命令。

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; gg。