string MATCHALL

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

string MATCHALL

Edoardo Pasca
Dear all, 

I'm trying to determine the active conda environment (name and path) at cmake runtime. 

To do that I issue the command
conda env list, which yields lines with the active environment highlighted with an asterisk: something similar to this:

env1       /path/to/env1
env2       / path/to/env2
env3   *  /path/to/env3

It is a perfect task for a match of REGEX but I cannot get it right and I defaulted to using brute force to achieve my goal.

My brute force method is 
1) splitting the lines by replacing \n with ;
2) now that I have a LIST (or I think that is a list) I can run a foreach to see if line-by-line I match the following string(REGEX MATCHALL "(.+)[*](.+)" match ${line})

Now, I'd expect to find in ${match} a list with the captured strings, i.e. env3;/path/to/env3. However that's not the case ${match} contains the whole line. So now I REPLACE "*" with ";" and now I have a almost LIST. Actually I do this:

string(REPLACE "*" ";" ENV_DIR ${match})
list (APPEND cc "")
foreach(conda ${ENV_DIR})
     string(STRIP ${conda} stripped)
     list(APPEND cc ${stripped})
endforeach()

Finally I've got my list ${cc} with the name and path of the environment as first and second element.

The question is, how would I use MATCHALL to achieve my goal (if possible)?

for the record I attach here the whole script.

Thanks for your help

Edo


execute_process(COMMAND "conda" "env" "list"
OUTPUT_VARIABLE _CONDA_ENVS
RESULT_VARIABLE _CONDA_RESULT
ERROR_VARIABLE _CONDA_ERR)
if(NOT _CONDA_RESULT)
string(REPLACE "\n" ";" ENV_LIST ${_CONDA_ENVS})
foreach(line ${ENV_LIST})
  string(REGEX MATCHALL "(.+)[*](.+)" match ${line})
  if (NOT ${match} EQUAL "")
    string(REPLACE "*" ";" ENV_DIR ${match})
    list (APPEND cc "")
    foreach(conda ${ENV_DIR})
      string(STRIP ${conda} stripped)
                                       list(APPEND cc ${stripped})
    endforeach()
    list(LENGTH cc Ns)
                                    if (${Ns} EQUAL 2)
      list(GET cc 0 CONDA_ENVIRONMENT)
      list(GET cc 1 CONDA_ENVIRONMENT_PATH)
    endif()
  endif()
endforeach()
else()
message(FATAL_ERROR "ERROR with conda command " ${_CONDA_ERR})
endif()


--
Edo
I know you think you understand what you thought I said, but I'm not sure you realize that what you heard is not what I meant (prob. Alan Greenspan)
:wq

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: string MATCHALL

CMake mailing list
In case it helps: the GUI can contains a regex explorer where you can play with rexexes. But beware: there are some corner cases around quoting special things.

I'd suggest to use if( ... MATCHES ...) And read the match from the CMAKE_MATCH_<n> variables.


On Oct 24, 2017 11:16, "Edoardo Pasca" <[hidden email]> wrote:
Dear all, 

I'm trying to determine the active conda environment (name and path) at cmake runtime. 

To do that I issue the command
conda env list, which yields lines with the active environment highlighted with an asterisk: something similar to this:

env1       /path/to/env1
env2       / path/to/env2
env3   *  /path/to/env3

It is a perfect task for a match of REGEX but I cannot get it right and I defaulted to using brute force to achieve my goal.

My brute force method is 
1) splitting the lines by replacing \n with ;
2) now that I have a LIST (or I think that is a list) I can run a foreach to see if line-by-line I match the following string(REGEX MATCHALL "(.+)[*](.+)" match ${line})

Now, I'd expect to find in ${match} a list with the captured strings, i.e. env3;/path/to/env3. However that's not the case ${match} contains the whole line. So now I REPLACE "*" with ";" and now I have a almost LIST. Actually I do this:

string(REPLACE "*" ";" ENV_DIR ${match})
list (APPEND cc "")
foreach(conda ${ENV_DIR})
     string(STRIP ${conda} stripped)
     list(APPEND cc ${stripped})
endforeach()

Finally I've got my list ${cc} with the name and path of the environment as first and second element.

The question is, how would I use MATCHALL to achieve my goal (if possible)?

for the record I attach here the whole script.

Thanks for your help

Edo


execute_process(COMMAND "conda" "env" "list"
OUTPUT_VARIABLE _CONDA_ENVS
RESULT_VARIABLE _CONDA_RESULT
ERROR_VARIABLE _CONDA_ERR)
if(NOT _CONDA_RESULT)
string(REPLACE "\n" ";" ENV_LIST ${_CONDA_ENVS})
foreach(line ${ENV_LIST})
  string(REGEX MATCHALL "(.+)[*](.+)" match ${line})
  if (NOT ${match} EQUAL "")
    string(REPLACE "*" ";" ENV_DIR ${match})
    list (APPEND cc "")
    foreach(conda ${ENV_DIR})
      string(STRIP ${conda} stripped)
                                       list(APPEND cc ${stripped})
    endforeach()
    list(LENGTH cc Ns)
                                    if (${Ns} EQUAL 2)
      list(GET cc 0 CONDA_ENVIRONMENT)
      list(GET cc 1 CONDA_ENVIRONMENT_PATH)
    endif()
  endif()
endforeach()
else()
message(FATAL_ERROR "ERROR with conda command " ${_CONDA_ERR})
endif()


--
Edo
I know you think you understand what you thought I said, but I'm not sure you realize that what you heard is not what I meant (prob. Alan Greenspan)
:wq

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: string MATCHALL

Edoardo Pasca
Gregor big thanks!

I had not understood that the matched string are stored in CMAKE_MATCH_<n> variables with:
n=0 for the whole string
n=1 the first match
n=2 the second match
etc

My script has now simplified consistently

                                  string(REGEX MATCHALL "(.+)[*](.+)" match ${line})
  if (NOT ${match} EQUAL "")
        string(STRIP ${CMAKE_MATCH_1} CONDA_ENVIRONMENT)
string(STRIP ${CMAKE_MATCH_2} CONDA_ENVIRONMENT_PATH)   
  endif()

Thanks again

Edo

On Tue, Oct 24, 2017 at 9:52 PM, Gregor Jasny <[hidden email]> wrote:
In case it helps: the GUI can contains a regex explorer where you can play with rexexes. But beware: there are some corner cases around quoting special things.

I'd suggest to use if( ... MATCHES ...) And read the match from the CMAKE_MATCH_<n> variables.


On Oct 24, 2017 11:16, "Edoardo Pasca" <[hidden email]> wrote:
Dear all, 

I'm trying to determine the active conda environment (name and path) at cmake runtime. 

To do that I issue the command
conda env list, which yields lines with the active environment highlighted with an asterisk: something similar to this:

env1       /path/to/env1
env2       / path/to/env2
env3   *  /path/to/env3

It is a perfect task for a match of REGEX but I cannot get it right and I defaulted to using brute force to achieve my goal.

My brute force method is 
1) splitting the lines by replacing \n with ;
2) now that I have a LIST (or I think that is a list) I can run a foreach to see if line-by-line I match the following string(REGEX MATCHALL "(.+)[*](.+)" match ${line})

Now, I'd expect to find in ${match} a list with the captured strings, i.e. env3;/path/to/env3. However that's not the case ${match} contains the whole line. So now I REPLACE "*" with ";" and now I have a almost LIST. Actually I do this:

string(REPLACE "*" ";" ENV_DIR ${match})
list (APPEND cc "")
foreach(conda ${ENV_DIR})
     string(STRIP ${conda} stripped)
     list(APPEND cc ${stripped})
endforeach()

Finally I've got my list ${cc} with the name and path of the environment as first and second element.

The question is, how would I use MATCHALL to achieve my goal (if possible)?

for the record I attach here the whole script.

Thanks for your help

Edo


execute_process(COMMAND "conda" "env" "list"
OUTPUT_VARIABLE _CONDA_ENVS
RESULT_VARIABLE _CONDA_RESULT
ERROR_VARIABLE _CONDA_ERR)
if(NOT _CONDA_RESULT)
string(REPLACE "\n" ";" ENV_LIST ${_CONDA_ENVS})
foreach(line ${ENV_LIST})
  string(REGEX MATCHALL "(.+)[*](.+)" match ${line})
  if (NOT ${match} EQUAL "")
    string(REPLACE "*" ";" ENV_DIR ${match})
    list (APPEND cc "")
    foreach(conda ${ENV_DIR})
      string(STRIP ${conda} stripped)
                                       list(APPEND cc ${stripped})
    endforeach()
    list(LENGTH cc Ns)
                                    if (${Ns} EQUAL 2)
      list(GET cc 0 CONDA_ENVIRONMENT)
      list(GET cc 1 CONDA_ENVIRONMENT_PATH)
    endif()
  endif()
endforeach()
else()
message(FATAL_ERROR "ERROR with conda command " ${_CONDA_ERR})
endif()


--
Edo
I know you think you understand what you thought I said, but I'm not sure you realize that what you heard is not what I meant (prob. Alan Greenspan)
:wq

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake




--
Edo
I know you think you understand what you thought I said, but I'm not sure you realize that what you heard is not what I meant (prob. Alan Greenspan)
:wq

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake