2010-10-05 26 views

Respuesta

22

Si por "expresiones regulares normales" que quiere decir Compatibles con Perl expresiones regulares (PCRE), entonces la ayuda de Vim proporciona un buen resumen de las diferencias entre las expresiones regulares de Vim y Perl:

:help perl-patterns 

Esto es lo que dice como de Vim 7.2:

 
9. Compare with Perl patterns       *perl-patterns* 

Vim's regexes are most similar to Perl's, in terms of what you can do. The 
difference between them is mostly just notation; here's a summary of where 
they differ: 

Capability      in Vimspeak  in Perlspeak ~ 
---------------------------------------------------------------- 
force case insensitivity  \c    (?i) 
force case sensitivity   \C    (?-i) 
backref-less grouping   \%(atom\)  (?:atom) 
conservative quantifiers  \{-n,m}   *?, +?, ??, {}? 
0-width match     atom\@=   (?=atom) 
0-width non-match    atom\@!   (?!atom) 
0-width preceding match   atom\@<=  (?<=atom) 
0-width preceding non-match  atom\@<!  (?!atom) 
match without retry    atom\@>   (?>atom) 

Vim and Perl handle newline characters inside a string a bit differently: 

In Perl,^and $ only match at the very beginning and end of the text, 
by default, but you can set the 'm' flag, which lets them match at 
embedded newlines as well. You can also set the 's' flag, which causes 
a . to match newlines as well. (Both these flags can be changed inside 
a pattern using the same syntax used for the i flag above, BTW.) 

On the other hand, Vim's^and $ always match at embedded newlines, and 
you get two separate atoms, \%^ and \%$, which only match at the very 
start and end of the text, respectively. Vim solves the second problem 
by giving you the \_ "modifier": put it in front of a . or a character 
class, and they will match newlines as well. 

Finally, these constructs are unique to Perl: 
- execution of arbitrary code in the regex: (?{perl code}) 
- conditional expressions: (?(condition)true-expr|false-expr) 

...and these are unique to Vim: 
- changing the magic-ness of a pattern: \v \V \m \M 
    (very useful for avoiding backslashitis) 
- sequence of optionally matching atoms: \%[atoms] 
- \& (which is to \| what "and" is to "or"; it forces several branches 
    to match at one spot) 
- matching lines/columns by number: \%5l \%5c \%5v 
- setting the start and end of the match: \zs \ze 
3

Pregunta demasiado amplia. Ejecute vim y escriba :help pattern.

14

"Expresión regular" realmente define algoritmos, no una sintaxis. Lo que eso significa es que los diferentes sabores de expresiones regulares utilizarán caracteres diferentes para que signifiquen lo mismo; o prefijarán algunos caracteres especiales con barras diagonales inversas donde otros no lo hacen. Por lo general, seguirán funcionando de la misma manera.

Érase una vez, POSIX defined the Basic Regular Expression sintaxis (BRE), que Vim sigue en gran medida. Poco después, también se lanzó una propuesta de sintaxis Expresión Regular Extendida (ERE). La principal diferencia entre los dos es que BRE tiende a tratar más caracteres como literales: una "a" es una "a", pero también un "(" es un "(", no es un carácter especial) y por lo tanto implica más barras invertidas para darles un significado "especial"

La discusión de diferencias complejas entre Vim y Perl en un comentario separado aquí es útil, pero también vale la pena mencionar algunas de las formas más simples en que las expresiones regulares de Vim difieren de la norma "aceptada" . (. por lo que es probable que decir Perl) Como se mencionó anteriormente, que en su mayoría se diferencian en su uso de una barra invertida

Estos son algunos ejemplos obvios:

 
Perl Vim  Explanation 
--------------------------- 
x?  x\=  Match 0 or 1 of x 
x+  x\+  Match 1 or more of x 
(xyz) \(xyz\) Use brackets to group matches 
x{n,m} x\{n,m} Match n to m of x 
x*?  x\{-} Match 0 or 1 of x, non-greedy 
x+?  x\{-1,} Match 1 or more of x, non-greedy 
\b  \< \> Word boundaries 
$n  \n  Backreferences for previously grouped matches 

Eso le da una idea de las diferencias más importantes. Pero si estás haciendo algo más complicado que lo básico, te sugiero que siempre asumas que Vim-regex va a ser diferente de Perl-regex o Javascript-regex y consulta algo como el Vim Regex website.

+0

Impresionante, gracias. Esto me dispara todo el tiempo! –

+0

esta fue la mejor respuesta y me ayudó a descubrir exactamente lo que estaba buscando gracias. – jimh

2

Pruebe el modo de expresión mágica de Vim. Se comporta más como la expresión regular tradicional, simplemente antepone tu patrón con \v. Vea :help /\v para más información. Lo amo.

+0

por regex tradicional ¿te refieres a expresiones regulares BRE o PCRE? –

+1

pcre :) more texttext – butterywombat

Cuestiones relacionadas