I want to extract the the substring Python 3.11 from the strings like Python 3.11.2.release0 (i.e. I want to extract Python followed by the first two digits separated by a .). Such longer strings often come from system output of python --version. Hence, my attempt is the following:

var myvar = system('python --version') -> filter("v:val =~ 'Python\ \d\+.\d\+'")

but it always result in an empty myvar.

Interestingly, if I have a buffer that contains the string Python 3.11.2.release0 and I type /Python\ \d\+.\d\+ the pattern Python 3.11 is correctly highlighted (I have incsearch set).

I am interested in Vim9.

EDIT: the utilisation of systemlist instead of system produce the same result.

1 Answer 1

  1. A space is not allowed between -> and the name of the function. Fixed:

    var myvar = system('python --version')->filter("v:val =~ 'Python\ \d\+.\d\+'")
  2. Whether you are interested in vim9script or not is irrelevant, here, as the functions work the same. This variant of your snippet makes that pointless requirement disappear:

    :echo system('python --version')->filter("v:val =~ 'Python\ \d\+.\d\+'")
  3. :help filter() is not at all adapted to what you are trying to do. Use :help matchstr() instead:

    :echo system('python --version')->matchstr('Python\ \d\+\.\d\+')
  • Auch! matchstr() !!!
    – Barzi2001
    Commented May 28, 2023 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.