Add half translted document.
This commit is contained in:
parent
722bd54766
commit
4df4a0b35a
459
doc/eregex.txt
Normal file
459
doc/eregex.txt
Normal file
@ -0,0 +1,459 @@
|
|||||||
|
*eregex.txt*
|
||||||
|
|
||||||
|
File: eregex.vim, eregex_e.vim
|
||||||
|
Author: AKUTSU toshiyuki <locrian@mbd.ocn.ne.jp>
|
||||||
|
Maintainer: othree <othree@gmail.com>
|
||||||
|
Version: 2.56
|
||||||
|
Required: Vim version 6.1
|
||||||
|
Note: eregex.vim is used to convert regexp notation.
|
||||||
|
eregex_e.vim is used to map command for eregex.vim.
|
||||||
|
|
||||||
|
1. License |eregex-license-to-use|
|
||||||
|
2. Installation |eregex-installations|
|
||||||
|
3. Functions |eregex-functions|
|
||||||
|
4. Command |eregex-commands|
|
||||||
|
5. Usage |eregex-examples|
|
||||||
|
6. Keymap |eregex-keymappings|
|
||||||
|
7. Principle |eregex-principle|
|
||||||
|
8. Convert Table |eregex-table|
|
||||||
|
9. Options |eregex-options|
|
||||||
|
10. Multiline |eregex-multiline|
|
||||||
|
11. Limitation of Delimiter |eregex-limitation-of-delimiter|
|
||||||
|
12. About Vim RegExp |eregex-about-vimregex|
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
1. License *eregex-license-to-use*
|
||||||
|
|
||||||
|
Copyright of eregex.vim and eregex_e.vim belongs to AKUTSU toshiyuki.
|
||||||
|
It is free to change and redistribute this script. You can think as an
|
||||||
|
GPL License software.
|
||||||
|
|
||||||
|
Author will not take any reponsibility for damages due to using this
|
||||||
|
script (eregex.vim, eregex_e.vim).
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
2. Installation *eregex-installations*
|
||||||
|
|
||||||
|
Open eregex.vba using Vim. And execute the following command.
|
||||||
|
>
|
||||||
|
:so %
|
||||||
|
<
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
3. Functions *eregex-functions* *eregex*
|
||||||
|
*E2v()*
|
||||||
|
E2v({extendedregex} [, {iISCDMm}])
|
||||||
|
|
||||||
|
Vim regexp notation will return.
|
||||||
|
>
|
||||||
|
:let vimregex = E2v('(?<=abc),\d+,(?=xzy)','i')
|
||||||
|
:echo vimregex
|
||||||
|
\c\%(abc\)\@<=,\d\+,\%(xzy\)\@=
|
||||||
|
<
|
||||||
|
Detail of option value can be found at |eregex-options|
|
||||||
|
or |eregex-multiline|
|
||||||
|
|
||||||
|
E2v("","V")
|
||||||
|
Return eregex.vim version number
|
||||||
|
>
|
||||||
|
:echo E2v('','V')
|
||||||
|
248
|
||||||
|
<
|
||||||
|
E2v({replacement}, {R1,R2,R3})
|
||||||
|
Return the "to" part of :S/pattern/to/ .
|
||||||
|
>
|
||||||
|
E2v('\r,\n,\&,&,\~,~', 'R1') => \n,\r,\&,&,\~,~
|
||||||
|
E2v('\r,\n,\&,&,\~,~', 'R2') => \r,\n,&,\&,~,\~
|
||||||
|
E2v('\r,\n,\&,&,\~,~', 'R3') => \n,\r,&,\&,~,\~
|
||||||
|
<
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
4. Command *eregex-commands*
|
||||||
|
*:E2v*
|
||||||
|
:[range]E2v [iISCDMm]
|
||||||
|
Extended regex To Vim regex.
|
||||||
|
Replace each extended-regex in [range] with vim-style-regex.
|
||||||
|
|
||||||
|
*:M*
|
||||||
|
:M/eregex[/{offset} [iISCDMm]]
|
||||||
|
Match
|
||||||
|
:M/<span class="foo">.*?<\/span>/Im
|
||||||
|
==> /\C<span class="foo">\_.\{-}<\/span>
|
||||||
|
|
||||||
|
*:S*
|
||||||
|
:[range]S/{eregex}/{string}/[&cegpriISCDMm]
|
||||||
|
Substitute
|
||||||
|
:'<,'>S/(\d{1,3})(?=(\d\d\d)+($|\D))/\1,/g
|
||||||
|
==> :'<,'>s/\(\d\{1,3}\)\%(\(\d\d\d\)\+\($\|\D\)\)\@=/\1,/g
|
||||||
|
|
||||||
|
*:G* *:G!*
|
||||||
|
:[range]G/{eregex}/{command}
|
||||||
|
:[range]G!/{eregex}/{command}
|
||||||
|
Global
|
||||||
|
:G/<<-(["'])?EOD\1/,/^\s*EOD\>/:left 8
|
||||||
|
==> :g/<<-\(["']\)\=EOD\1/,/^\s*EOD\>/:left 8
|
||||||
|
|
||||||
|
*:V*
|
||||||
|
:[range]V/{eregex}/{command}
|
||||||
|
Vglobal
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
5. Usage *eregex-examples*
|
||||||
|
|
||||||
|
(1) :E2v command
|
||||||
|
|
||||||
|
Change the regexp notation of the cursor line.
|
||||||
|
|
||||||
|
(\d{1,3})(?=(\d\d\d)+($|\D))
|
||||||
|
|
||||||
|
Move cursor to this line and execute :E2v command will change this line to
|
||||||
|
the following result.
|
||||||
|
|
||||||
|
\(\d\{1,3}\)\%(\(\d\d\d\)\+\($\|\D\)\)\@=
|
||||||
|
|
||||||
|
|
||||||
|
(2) :M command
|
||||||
|
>
|
||||||
|
:M/<Items\s+attr="media">.+?<\/Items>/Im
|
||||||
|
<
|
||||||
|
:normal! /\C<Items[ \t\r\n^L]\+attr="media">\_.\{-1,}<\/Items>
|
||||||
|
|
||||||
|
<Items attr="media">
|
||||||
|
<item name="cdrom" price="90" />
|
||||||
|
<item name="cdrw" price="500" />
|
||||||
|
<item name="dvd" price="1000" />
|
||||||
|
</Items>
|
||||||
|
|
||||||
|
|
||||||
|
(3) :S command
|
||||||
|
>
|
||||||
|
:'<,'>S/(\d{1,3})(?=(\d\d\d)+($|\D))/\1,/g
|
||||||
|
<
|
||||||
|
:'<,'>s/\(\d\{1,3}\)\%(\(\d\d\d\)\+\($\|\D\)\)\@=/\1,/g
|
||||||
|
|
||||||
|
1 --> 1
|
||||||
|
12 --> 12
|
||||||
|
123 --> 123
|
||||||
|
1234 --> 1,234
|
||||||
|
12345 --> 12,345
|
||||||
|
123456 --> 123,456
|
||||||
|
1234567 --> 1,234,567
|
||||||
|
12345678 --> 12,345,678
|
||||||
|
123456789 --> 123,456,789
|
||||||
|
|
||||||
|
(4) :G command
|
||||||
|
>
|
||||||
|
:G/^begin$/+1;/^end$/-1:S/\l+/\U&/g
|
||||||
|
<
|
||||||
|
:g/^begin$/+1;/^end$/-1:s/\l\+/\U&/g
|
||||||
|
|
||||||
|
begin
|
||||||
|
hello world.
|
||||||
|
hello world wide web.
|
||||||
|
hello The Internet.
|
||||||
|
end
|
||||||
|
|
||||||
|
|begin
|
||||||
|
| HELLO WORLD.
|
||||||
|
| HELLO WORLD WIDE WEB.
|
||||||
|
| HELLO THE INTERNET.
|
||||||
|
|end
|
||||||
|
|
||||||
|
(5) :V command
|
||||||
|
Skipped.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
6. keymap *eregex-keymappings*
|
||||||
|
|
||||||
|
You can add the following keymap to use / instead of type :/M
|
||||||
|
|
||||||
|
nnoremap / :M/
|
||||||
|
nnoremap ? :M?
|
||||||
|
nnoremap ,/ /
|
||||||
|
nnoremap ,? /
|
||||||
|
|
||||||
|
"/" will use :M/ to search. ",/" will use the original "/".
|
||||||
|
|
||||||
|
--------------------
|
||||||
|
Add the following line to ~/.vimrc
|
||||||
|
let eregex_replacement=3
|
||||||
|
will make :S have the following rules.
|
||||||
|
|
||||||
|
:S/pattern/\r,\n,\&,&,\~,~/
|
||||||
|
:s/pattern/\n,\r,&,\&,~,\~/
|
||||||
|
|
||||||
|
+--------------------+-----------------------------+
|
||||||
|
| eregex_replacement | :S/pattern/\n,\r,&,\&,~,\~/ |
|
||||||
|
+--------------------+-----------------------------+
|
||||||
|
| 0 | :s/pattern/\n,\r,&,\&,~,\~/ |
|
||||||
|
| 1 | :s/pattern/\r,\n,&,\&,~,\~/ |
|
||||||
|
| 2 | :s/pattern/\n,\r,\&,&,\~,~/ |
|
||||||
|
| 3 | :s/pattern/\r,\n,\&,&,\~,~/ |
|
||||||
|
+--------------------+-----------------------------+
|
||||||
|
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
7. Principle *eregex-principle*
|
||||||
|
eregex.vim adopts the way of extended regex about "alternation",
|
||||||
|
"repetition" and "grouping".
|
||||||
|
eregex.vim において、
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
8. Convert Table *eregex-table*
|
||||||
|
|
||||||
|
Perl notation on left side. Vim notation('magic') on right side.
|
||||||
|
|
||||||
|
Alternation
|
||||||
|
--------------------
|
||||||
|
:M/a|b /a\|b
|
||||||
|
|
||||||
|
Repetition
|
||||||
|
--------------------
|
||||||
|
:M/a* /a*
|
||||||
|
:M/a+ /a\+
|
||||||
|
:M/a? /a\=
|
||||||
|
|
||||||
|
:M/a*? /a\{-}
|
||||||
|
:M/a+? /a\{-1,}
|
||||||
|
:M/a?? /a\{-,1}
|
||||||
|
|
||||||
|
:M/a{3,5} /a\{3,5}
|
||||||
|
:M/a{3,} /a\{3,}
|
||||||
|
:M/a{,5} /a\{,5}
|
||||||
|
|
||||||
|
:M/a{3,5}? /a\{-3,5}
|
||||||
|
:M/a{3,}? /a\{-3,}
|
||||||
|
:M/a{,5}? /a\{-,5}
|
||||||
|
|
||||||
|
Grouping
|
||||||
|
--------------------
|
||||||
|
:M/(abc) /\(abc\)
|
||||||
|
:M/(?:abc) /\%(abc\)
|
||||||
|
:M/(?<=abc) /\%(abc\)\@<=
|
||||||
|
:M/(?<!abc) /\%(abc\)\@<!
|
||||||
|
:M/(?=abc) /\%(abc\)\@=
|
||||||
|
:M/(?!abc) /\%(abc\)\@!
|
||||||
|
:M/(?>abc) /\%(abc\)\@>
|
||||||
|
|
||||||
|
Special Characters
|
||||||
|
--------------------
|
||||||
|
:M/\\,\|,\(,\),\{,\},\?,\+,\*,\[,\] /\\,|,(,),{,},?,+,\*,\[,\]
|
||||||
|
:M/\^,\$ /\^,\$
|
||||||
|
|
||||||
|
Not support
|
||||||
|
--------------------
|
||||||
|
\A, \b, \B, \G, \Z, \z
|
||||||
|
Vim doesn't support these features.
|
||||||
|
(?i:a) and (?-i) neither.
|
||||||
|
|
||||||
|
Vim の正規表現で使えないもの
|
||||||
|
--------------------
|
||||||
|
\%(re\) 等、パレン(丸カッコ) を使うもの全般。
|
||||||
|
~ matches the last given substitute string
|
||||||
|
\m 'magic' on for the following chars in the pattern
|
||||||
|
\M 'magic' off for the following chars in the pattern
|
||||||
|
\v the following chars in the pattern are "very magic"
|
||||||
|
\V the following chars in the pattern are "very nomagic"
|
||||||
|
|
||||||
|
\x hex digit: [0-9A-Fa-f]
|
||||||
|
\\x[0-9A-Fa-f]{1,2} の場合、文字そのものに変換する。
|
||||||
|
\x82\xa0 => 'あ' ( shift-jis, cp932 )
|
||||||
|
ただし、0x00 と 0x0a と 0x08 は変換しません。
|
||||||
|
|
||||||
|
Vim の正規表現で使えるもの
|
||||||
|
--------------------
|
||||||
|
大抵使えます。(^^;)
|
||||||
|
\d, \D, \w, \W, \s, \S, \a, \A, \u, \U, \b, ...
|
||||||
|
\<, \>, \zs, \ze
|
||||||
|
\_[a-z], \%[abc], [[:alpha:]], \_., \_^, \_$
|
||||||
|
\%23l, \%23c, \%23v, \%#
|
||||||
|
など。
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
9. 特殊なオプションとアトム *eregex-options*
|
||||||
|
Note: "^L" は \x0c
|
||||||
|
|
||||||
|
eregex.vim Vim
|
||||||
|
---------------------------------------
|
||||||
|
:M/a/i /\ca/
|
||||||
|
:M/\ca/ /\ca/
|
||||||
|
:M/a/I /\Ca/
|
||||||
|
:M/\Ca/ /\Ca/
|
||||||
|
|
||||||
|
:M/\s/S /[ \t\r\n^L]
|
||||||
|
:M/\S/S /[^ \t\r^L]
|
||||||
|
:M/[^az]/C /\_[^az]/
|
||||||
|
:M/\W/C /\_W/
|
||||||
|
:M/./D /\_./
|
||||||
|
|
||||||
|
:M/\s[^az]./M /[ \t\r\n^L]\_[^az]./
|
||||||
|
:M/\s[^az].\M/ 同上。
|
||||||
|
|
||||||
|
:M/\s[^az]./m /[ \t\r\n^L]\_[^az]\_./
|
||||||
|
:M/\s[^az].\m/ 同上。
|
||||||
|
|
||||||
|
+--------+------+--------------------------------------------------------+
|
||||||
|
| OPTION | ATOM | 説明 |
|
||||||
|
+--------+------+--------------------------------------------------------+
|
||||||
|
| /i | \c | 大小文字の区別無し。 |
|
||||||
|
| /I | \C | 大小文字の区別あり。 |
|
||||||
|
+--------+------+--------------------------------------------------------+
|
||||||
|
| /S | | \s および \S を [ \t\r\n^L] や [^ \t\r^L] に変換する。 |
|
||||||
|
| /C | | 補集合が改行にもマッチする。 |
|
||||||
|
| /D | | ドットが改行にもマッチする。 |
|
||||||
|
+--------+------+--------------------------------------------------------+
|
||||||
|
| /M | \M | /S と /C を行なう。 部分マルチライン。 |
|
||||||
|
| /m | \m | /S と /C と /D を行なう。完全マルチライン。 |
|
||||||
|
+--------+------+--------------------------------------------------------+
|
||||||
|
|
||||||
|
Note:
|
||||||
|
(A) オプション /iImM
|
||||||
|
(B) \c, \C, \m, \M
|
||||||
|
(C) (?i), (?I), (?m), (?M)
|
||||||
|
これらが同時に指定された場合、上の方が優先順位が高い。
|
||||||
|
ちなみに、(?M) と (?m) は、ブラケットの中の \s には適用されません。
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
10. マルチライン *eregex-multiline*
|
||||||
|
|
||||||
|
とりあえず以下の表を見て。(^^;)
|
||||||
|
+-----+----------------------------------------------+--------------------+
|
||||||
|
| Num | eregex.vim => vim regex | ruby regex |
|
||||||
|
+-----+----------------------------------------------+--------------------+
|
||||||
|
| (1) | :M/a\s[^az].z/ => /a\s[^az].z/ | /a[ \t][^az\n].z/ |
|
||||||
|
+-----+----------------------------------------------+--------------------+
|
||||||
|
| | :M/a\s[^az].z/S => /a[ \t\r\n^L][^az].z/ | /a\s[^az\n].z/ |
|
||||||
|
| | :M/a\s[^az].z/C => /a\s\_[^az].z/ | /a[ \t][^az].z/ |
|
||||||
|
| | :M/a\s[^az].z/D => /a\s[^az]\_.z/ | /a[ \t][^az\n].z/m |
|
||||||
|
+-----+----------------------------------------------+--------------------+
|
||||||
|
| (2) | :M/a\s[^az].z/M => /a[ \t\r\n^L]\_[^az].z/ | /a\s[^az].z/ |
|
||||||
|
| (3) | :M/a\s[^az].z/m => /a[ \t\r\n^L]\_[^az]\_.z/ | /a\s[^az].z/m |
|
||||||
|
+-----+----------------------------------------------+--------------------+
|
||||||
|
|
||||||
|
(1) は、「文字クラス」が Vim 流。
|
||||||
|
(2) は、「文字クラス」が Ruby 流。
|
||||||
|
Vim 流に言えば、改行にマッチする所が増えたので、部分マルチライン。
|
||||||
|
Ruby 流に言えば、マルチラインでない。よってオプションは大文字の M 。
|
||||||
|
(3) は、いわゆる Ruby 流のマルチライン。
|
||||||
|
|
||||||
|
Note:
|
||||||
|
Vim の正規表現では、/[^az]/ は改行にマッチしません。
|
||||||
|
改行にマッチしないことを明示して /[^az\n]/ と書く必要はありません。
|
||||||
|
/[^az\n]/ は意図に反して改行にもマッチします。
|
||||||
|
よって、本来 /[^ \t\r\n^L]/ とするべきところを、/[^ \t\r^L]/ に
|
||||||
|
変換している場合があります。
|
||||||
|
原則的に Vim では、[^...] の中に \n を書いてはいけません。
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
11. デリミタの制限 *eregex-limitation-of-delimiter*
|
||||||
|
|
||||||
|
:M で使える区切り文字は / だけです。
|
||||||
|
:S 、:G および :V で使える区切り文字は /, #, @ です。
|
||||||
|
これらの使用方法は :s 、:g 、:v と同じです。
|
||||||
|
制限も同様です。
|
||||||
|
|
||||||
|
区切り文字を @ にするといろいろ制限があります。
|
||||||
|
|
||||||
|
一見うまくいきそうに見えてダメな例。
|
||||||
|
"foo@bar.baz.co.jp" を "foo@hoge.co.jp" に置換しようとして、
|
||||||
|
>
|
||||||
|
:%s@\<foo\@bar\.baz\.co\.jp\>@foo\@hoge.co.jp@Ig
|
||||||
|
<
|
||||||
|
は、エラーです。
|
||||||
|
Vim の正規表現で、\@ は特別な扱いを受けています。
|
||||||
|
==============================================================================
|
||||||
|
12. Vim の正規表現について *eregex-about-vimregex*
|
||||||
|
|
||||||
|
以下 カーソルを "111,222,333" の行に置いて、:S... を実行してください。
|
||||||
|
|
||||||
|
(1)通常のサブマッチ。
|
||||||
|
|
||||||
|
111,222,333
|
||||||
|
>
|
||||||
|
:S/(\d+),(\d+),(\d+)/\=submatch(1) + submatch(2) + submatch(3)
|
||||||
|
<
|
||||||
|
666
|
||||||
|
|
||||||
|
(2)Vim 独自機能。
|
||||||
|
マッチデータ($&, &, matchdata) と、サブマッチを分離できます。
|
||||||
|
\zs と \ze を使う。 See :h /\zs
|
||||||
|
|
||||||
|
111,222,333
|
||||||
|
>
|
||||||
|
:S/(\d+),\zs\d+\ze,(\d+)/\=submatch(1) + submatch(0) + submatch(2)
|
||||||
|
<
|
||||||
|
111,666,333
|
||||||
|
|
||||||
|
|
||||||
|
(3)
|
||||||
|
以下の方が分かりやすいかも。
|
||||||
|
|
||||||
|
111,222,333
|
||||||
|
>
|
||||||
|
:S/(\d+),\zs(\d+)\ze,(\d+)/\=submatch(1) + submatch(2) + submatch(3)
|
||||||
|
<
|
||||||
|
111,666,333
|
||||||
|
|
||||||
|
\zs と \ze を使うと、マッチデータに含まれないサブマッチを操作できます。
|
||||||
|
|
||||||
|
(4) \_x の機能。
|
||||||
|
|
||||||
|
\u で [A-Z] を表す。
|
||||||
|
\_u で [A-Z\n] を表す。
|
||||||
|
\_[A-Z] は [A-Z\n] と同じ。
|
||||||
|
|
||||||
|
大文字以外で改行を含む文字。
|
||||||
|
\_U == \_[^A-Z]
|
||||||
|
|
||||||
|
\_. は改行を含む任意の文字。
|
||||||
|
|
||||||
|
|
||||||
|
(5) ^ と \_^ および $ と \_$ の違い。
|
||||||
|
|
||||||
|
$ を例にとります。
|
||||||
|
|
||||||
|
通常 $ は
|
||||||
|
(1)正規表現の一番最後。
|
||||||
|
(2) ) の直前。
|
||||||
|
(3) | の直前。
|
||||||
|
にある場合だけ行末を表す。
|
||||||
|
|
||||||
|
ところが任意の場所で行末を表せるのが \_$ 。
|
||||||
|
|
||||||
|
111,222,333
|
||||||
|
>
|
||||||
|
:S/(\d+),(\d+),(\d+)\zs\_$\ze/\=',' . (submatch(1) + submatch(2) + submatch(3))
|
||||||
|
<
|
||||||
|
111,222,333,666
|
||||||
|
|
||||||
|
ここで使っている \_$ の代わりに $ を使っても意図した結果になりません。
|
||||||
|
Note:
|
||||||
|
\_^ と \_$ は perl の /m オプションとは全然違います。
|
||||||
|
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
13. Changes
|
||||||
|
revision 2.56
|
||||||
|
(1) Add support for "?"
|
||||||
|
|
||||||
|
revision 2.55
|
||||||
|
(1) E2v() にバージョン番号や、sub-replace-special の置換を加えた。
|
||||||
|
(2) \v を 0x0b に置換するようにした。
|
||||||
|
(3) :M/pattern/ でマッチしなくても、@/ を更新した。
|
||||||
|
|
||||||
|
revision 2.35
|
||||||
|
(1) オプション S,C,D,M,m の追加と変更。
|
||||||
|
|
||||||
|
revision 1.4x
|
||||||
|
(1) :S/\x2f/\\/g とかすると、:s///\\/g に変換してしまうバグ修正。
|
||||||
|
(2) エスケープされたデリミタを検索履歴ではアンエスケープした。
|
||||||
|
デリミタが '@' の場合を除く。
|
||||||
|
(3) オプション m の修正。
|
||||||
|
|
||||||
|
revision 1.13
|
||||||
|
:G で ! を使えるようにした。
|
||||||
|
|
||||||
|
revision 1.1.1.21
|
||||||
|
:S の /c オプションで確認のプロンプトが見えなくなってしまう問題修正。
|
||||||
|
|
||||||
|
-- vim:ft=help:
|
Loading…
x
Reference in New Issue
Block a user