[CMake] FIND sub-command to the STRING command

Michael Wild themiwi at gmail.com
Fri Feb 4 04:56:42 EST 2011


On 02/04/2011 10:40 AM, Tim Hütz wrote:
> Hello,
> 
> recently I was searching for a CMake option to get the position of a single character (first appearance) out of a string. As I found nothing which could provide me an easy solution, I pulled the latest development version and added a FIND sub-command to the STRING command. I don't know if this could be helpful for anymore else, hence I attached a patch with this extension to CMake. I'm happy for any comments.
> 
> Best regards,
> Tim

I normally do it like this:

set(str "Hello Tim")
set(pos -1)
if(str MATCHES "^([^T]*)T")
  string(LENGTH "${CMAKE_MATCH_1}" pos)
endif()
message(STATUS "Position of first T is ${pos}")

If CMake also supported non-greedy regexes one could also find substrings.


Michael


More information about the CMake mailing list