1

Faced with the problem of parsing version of clang for different vendors clang --version|head -1

Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn) => 3.5
FreeBSD clang version 3.4.1 (tags/RELEASE_34/dot1-final 208032) 20140512 => 3.4.1
clang version 3.5.0 (tags/RELEASE_350/rc2) => 3.5.0

Now have this regular expression

match: (clang version|based on LLVM)\ (\d+)(\.\d+)(\.\d+)?
to \2\3\4

I need to exclude (clang version|based on LLVM) from the result match() - \1.

Denis Denisov
  • 661
  • 8
  • 17

2 Answers2

2

Ok, since the tool you use is based on PCRE, this pattern should work:

(?m)(?:^Apple .*)?\K \d+(?:\.\d+){1,2}

The (?m) is not useful in real life because you only test one line, so you can remove it.

The \K removes all that have been matched on the left from the match result.

If you use another language/tool where the \K feature is not available you can use a capturing group to extract the information you want from the whole match result:

(?:^Apple .*)? ([0-9]+(?:[.][0-9]+){1,2})

With cmake:

string(REGEX REPLACE "(^Apple .*)? ([0-9]+([.][0-9]+){1,2}).*" "\\2" CLANG_VERSION "${_clang_version_info}")
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
2

Your question looks like you're expecting CMake's regex handling to be Perl-like, but it's pretty different.

In CMake syntax, the following should do what you want:

if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
  execute_process(COMMAND clang --version COMMAND head -1 OUTPUT_VARIABLE ClangVersionLine)
  string(REGEX MATCH "(clang version|based on LLVM) ([0-9]\\.[0-9]\\.?[0-9]?)" UnusedVar "${ClangVersionLine}")
  set(ClangVersion "${CMAKE_MATCH_2}")
  message("ClangVersion - ${ClangVersion}")
endif()
Fraser
  • 74,704
  • 20
  • 238
  • 215