2010-10-05 17 views

risposta

22

Se per "regex normale" si intende Perl Compatible Regular Expressions (PCRE), quindi l'aiuto Vim fornisce una buona sintesi delle differenze tra regex di Vim e Perl:

:help perl-patterns 

Ecco cosa dice come di 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

Domanda troppo ampia. Esegui vim e digita :help pattern.

14

"Espressione regolare" definisce in realtà gli algoritmi, non una sintassi. Ciò significa che i diversi sapori delle espressioni regolari useranno caratteri diversi per indicare la stessa cosa; o prefisso alcuni caratteri speciali con barre inverse in cui altri non lo fanno. In genere funzionano ancora allo stesso modo.

Una volta, la sintassi POSIX defined the Basic Regular Expression (BRE), che Vim segue in gran parte. Molto presto, è stata rilasciata anche una proposta di sintassi dell'ERE (Extended Regular Expression). La principale differenza tra i due è che BRE tende a trattare più personaggi come letterali - un "a" è un "a", ma anche un "(" è un "(", "non un carattere speciale - e quindi implica più barre retroverse a Dare loro un significato "speciale"

La discussione di differenze complesse tra Vim e Perl in un commento separato qui è utile, ma vale anche la pena menzionare alcuni dei modi più semplici in cui Vim regexes differiscono dalla norma "accettata" . (. con la quale si intende probabilmente Perl) Come accennato in precedenza, la maggior parte essi si differenziano per il loro uso di una barra inversa precedente

Ecco alcuni esempi ovvi:

 
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 

Questo ti dà un assaggio delle differenze più importanti. Ma se stai facendo qualcosa di più complicato delle basi, ti suggerisco sempre di pensare che Vim-regex sia diverso da Perl-regex o Javascript-regex e consulta qualcosa come lo Vim Regex website.

+0

Fantastico, grazie. Questo mi fa incazzare ogni volta! –

+0

questa è stata la migliore risposta e mi ha aiutato a capire esattamente cosa stavo cercando grazie. – jimh

2

Prova la modalità di regex molto magica di Vim. Si comporta più come la regex tradizionale, ma antepone il modello con \v. Vedi :help /\v per maggiori informazioni. Lo adoro.

+0

con espressioni regex tradizionali intendi espressioni regolari di base (BRE) o PCRE? –

+1

pcre :) altro testo – butterywombat