eregex.vim/doc/eregex.txt

432 lines
14 KiB
Plaintext
Raw Normal View History

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