eregex.vim/doc/eregex.txt
2010-10-19 14:47:22 +08:00

432 lines
14 KiB
Plaintext
Raw Blame History

*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 style.
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 responsibility 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
<
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 style 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".
==============================================================================
8. Convert Table *eregex-table*
Perl style on left side. Vim style ('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.
Unusable Vim regexp notation
--------------------
\%(re\) and square rackets
~ 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} will change to character.
\x82\xa0 => '<27><>' ( shift-jis, cp932 )
But 0x00 and 0x0a and 0x08 will not change.
Usable Vim regexp notations
--------------------
\d, \D, \w, \W, \s, \S, \a, \A, \u, \U, \b, ...
\<, \>, \zs, \ze
\_[a-z], \%[abc], [[:alpha:]], \_., \_^, \_$
\%23l, \%23c, \%23v, \%#
==============================================================================
9. Special Option and Atom *eregex-options*
Note: "^L" is \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/ Same as above.
:M/\s[^az]./m /[ \t\r\n^L]\_[^az]\_./
:M/\s[^az].\m/ Same as above.
+--------+------+--------------------------------------------------------+
| OPTION | ATOM | NOTE |
+--------+------+--------------------------------------------------------+
| /i | \c | Case insensitive |
| /I | \C | Case sensitive |
+--------+------+--------------------------------------------------------+
| /S | | \s and \S becomes [ \t\r\n^L] and [^ \t\r^L] |
| /C | | [] brackets will also match line break. |
| /D | | Dot will match line break |
+--------+------+--------------------------------------------------------+
| /M | \M | Use /S and /C . Partial multiline support |
| /m | \m | Use /S, /C and /D . All support multiline |
+--------+------+--------------------------------------------------------+
Note:
(A) Option /iImM
(B) \c, \C, \m, \M
(C) (?i), (?I), (?m), (?M)
If you use these at the same time. The priority will follow the order.
If you use (?M) and (?m), \s in bracket will not work.
==============================================================================
10. Multiline *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) is Vim style character class.
(2) is Ruby style character class.
(3) is Ruby style multiline.
Note:
In Vim style regexp, /[^az]/ will not match break line.
But it is incorrect to to write /[^az\n]/ to point this out.
/[^az\n]/ will match breakline.
So we might use /[^ \t\r^L]/ instead of /[^ \t\r\n^L]/ .
Just remember not write \n in [^...] .
==============================================================================
11. Limitation of Delimiter *eregex-limitation-of-delimiter*
:M only support / and ? .
:S, :G and :V supports /, #, @ .
Usage and limitation are the same as :s, :g and :v
Delimiter use @ will have some more limitation.
For example:
To replace "foo@bar.baz.co.jp" with "foo@hoge.co.jp"
>
:%s@\<foo\@bar\.baz\.co\.jp\>@foo\@hoge.co.jp@Ig
<
Will fail.
\@ have special meaning in Vim regular expression .
==============================================================================
12. About Vim Regular Expression *eregex-about-vimregex*
The following samples are all in a line with content "111,222,333".
And use :S to execute replace.
(1)Regular submatch
111,222,333
>
:S/(\d+),(\d+),(\d+)/\=submatch(1) + submatch(2) + submatch(3)
<
666
(2)Vim special feature
Match data($&, &, matchdata) and submatch can be seperated.
Use \zs and \ze . See :h /\zs for more information.
111,222,333
>
:S/(\d+),\zs\d+\ze,(\d+)/\=submatch(1) + submatch(0) + submatch(2)
<
111,666,333
(3)
One more example
111,222,333
>
:S/(\d+),\zs(\d+)\ze,(\d+)/\=submatch(1) + submatch(2) + submatch(3)
<
111,666,333
Use \zs and \ze to control matchdata and submatch.
(4) \_x
\u is [A-Z]
\_u is [A-Z\n]
\_[A-Z] is [A-Z\n]
Not uppercase characters and line break.
\_U == \_[^A-Z]
\_. is any character including line break.
(5) Difference between ^ and \_^ or $ and \_$
Take $ for example
$ is used in the following position
(1) The last of regexp.
(2) Just before ) .
(3) Just before | .
\_$ can be used anywhere to present as a line break.
111,222,333
>
:S/(\d+),(\d+),(\d+)\zs\_$\ze/\=',' . (submatch(1) + submatch(2) + submatch(3))
<
111,222,333,666
If use $ instead of \_$ in this example will have different result.
Note:
\_^ and \_$ is totally different from /m option in Perl regexp.
==============================================================================
13. Changes
revision 2.56
(1) Add support for "?"
-- vim:ft=help: