Help us understand the problem. What is going on with this article?

vim scriptで、正規表現のマッチを変数に格納する

More than 5 years have passed since last update.

vim scriptで、正規表現のマッチを変数に格納するには、matchstr関数を使います。

matchStr1.vim
let defLine = "def some_method"
let methodName = matchstr(defLine, '\(def\s\+\)\zs\k\+', 0)
echo methodName 
"-> some_method

最初の引数はリストでも関数を呼ぶことはできますが、リストの場合はマッチした結果ではなく、該当行全体が返ってくるため、さらに絞り込む必要があります。

matchStr2.vim
let defFile = ["def move_camera"]
let defFile += [ "eye = [100, 1000, 1000]"]
let defFile += [ "target = [0, 0, 0]"]
let defFile += [ "up = [0, 0, 1]"]
let defFile += [ "my_camera= Sketchup::Camera.new eye, target, up"]  
let defFile += [ "view = Sketchup.active_model.active_view"]
let defFile += [ "view.camera = my_camera"]
let defFile += [ "end"]

let defLine = matchstr(defFile, '\(def\s\+\)\zs\k\+', 0)
let methodName = matchstr(defLine, '\(def\s\+\)\zs\k\+', 0)
echo methodName 
" -> move_camera
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした