4

HTMLファイルがあります。ファイル内のすべてのリンクを取得し、Vimを使用して別のファイルに保存したいと思います。

正規表現は次のようになることを私は知っています:

:g/href="\v([a-z_/]+)"/

でもここからどこへ行けばいいのかわかりません。

4

5 に答える 5

17

ジェフ・ミートボール・ヤンはほとんどそこにいました.

Sasha が書いたように、w を使用すると元のファイル全体が outfile に書き込まれます

一致した行のみを書き込むには、「.」を追加する必要があります。「w」の前:

:g/href="\v([a-z_/]+)"/ .w >> outfile

outfile が存在する必要があることに注意してください。

于 2011-02-02T15:55:33.907 に答える
3

クリア reg:x

qxq

検索regex(なんでも)してreg:xに追加

:g/regex/call setreg('X', matchstr(getline('.'), 'regex') . "\n")

新しいタブを開く

:tabnew outfile

reg:x を入れる

"xp

ファイルを書き込む

:w
于 2011-06-15T13:09:07.863 に答える
2

ここでの課題は、オンラインに複数ある可能性のあるすべてのリンクを抽出することです。それ以外の場合は、次のように簡単に実行できます。

" Extract all lines with href=
:g/href="[^"]\+"/w >> list_of_links.txt
" Open the new file
:e list_of_links.txt
" Extract the bit inside the quotation marks
:%s/.*href="\([^"]\+\)".*/\1/

最も簡単な方法は、おそらくこれを行うことです。

" Save as a new file name
:saveas list_of_links.txt
" Get rid of any lines without href=
:g!/href="\([^"]\+\)"/d
" Break up the lines wherever there is a 'href='
:%s/href=/\rhref=/g
" Tidy up by removing everything but the bit we want
:%s/^.*href="\([^"]\+\)".*$/\1/

あるいは(同様のテーマに従って)、

:g/href="[^"]\+"/w >> list_of_links.txt
:e list_of_links.txt
:%s/href=/\rhref=/g
:%s/^.*href="\([^"]\+\)".&$/\1/

(:help saveas、:help :vglobal、:help :s を参照)

ただし、より直接的な方法で本当にやりたい場合は、次のようにすることができます。

" Initialise register 'h'
:let @h = ""
" For each line containing href=..., get the line, and carry out a global search
" and replace that extracts just the URLs and a double quote (as a delimiter)
:g/href="[^"]\+"/let @h .= substitute(getline('.'), '.\{-}href="\([^"]\+\)".\{-}\ze\(href=\|$\)', '\1"', 'g')
" Create a new file
:new
" Paste the contents of register h (entered in normal mode)
"hp
" Replace all double quotes with new-lines
:s/"/\r/g
" Save
:w

最後に、for ループを使用して関数内で実行することもできますが、それは他の人に任せます。

于 2009-06-22T07:18:09.760 に答える
1

カーソルを最初の行/列に置き、これを試してください:

:redir > output.txt|while search('href="', "We")|exe 'normal yi"'|echo @"|endwhile|redir END
于 2009-06-22T22:36:48.560 に答える
0

これを試しましたか?

:g/href="\v([a-z_/]+)"/w >> 出力ファイル

于 2009-06-22T07:10:39.900 に答える