本文介绍了CMake中的版本正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想验证用户指定的版本字符串-确保它由三个以句点分隔的数字(例如1.20.300 )组成。 但是我不确定如何编写这样的正则表达式,下面的代码只是一个尝试:
I want to validate user-specified version string - to ensure it consists of three period-separated numbers (e.g. 1.20.300). But i'm not sure how to write such regex, the code below is just a try:
if( PROJECT_VERSION MATCHES "([0-9]+).([0-9]+).([0-9+])" ) message( "NOTE: Valid version string" ) else() message( FATAL_ERROR "Invalid version string" ) endif()那么,如何正确编写所需的正则表达式呢? 谢谢。
So, how to correctly write required regex? Thanks.
UPD 我的正则表达式也匹配 1.2.3.4 ,但不应该! 只能使用三个以句点分隔的数字。
UPD My regex also matches 1.2.3.4, but is should not! Only three period-separated numbers are possible.
推荐答案点在正则表达式中是特殊的,因此应转义它们:
Dots are special in regex, so you should escape them:
"^([0-9]+)\\.([0-9]+)\\.([0-9]+)$"为什么要使用双反斜杠?看到这里: stackoverflow/a/4490920/4323
Why double-backslash? See here: stackoverflow/a/4490920/4323
更多推荐
CMake中的版本正则表达式
发布评论