eregex.txt 13.6 KB
Newer Older
Murukesh Mohanan's avatar
Murukesh Mohanan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
*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  => '' ( 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: