2010-10-05 12 views

Antwort

22

Wenn von "normalen regex" Sie Perl-Compatible Regular Expressions (PCRE) bedeuten, dann ist die Vim-Hilfe bietet eine gute Zusammenfassung der Unterschiede zwischen Vims Regexes und Perl:

:help perl-patterns 

Hier ist, was er sagt, als von 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

Zu weite Frage. Führen Sie vim aus, und geben Sie :help pattern ein.

14

"Regulärer Ausdruck" definiert wirklich Algorithmen, keine Syntax. Das bedeutet, dass verschiedene Varianten regulärer Ausdrücke unterschiedliche Zeichen verwenden, um dasselbe zu bedeuten. oder sie werden einige Sonderzeichen mit Backslashes voranstellen, andere nicht. Sie arbeiten normalerweise immer noch auf die gleiche Weise.

Es war einmal, POSIX defined the Basic Regular Expression Syntax (BRE), die Vim weitgehend folgt. Sehr bald darauf wurde auch ein Extended Regular Expression (ERE) -Syntax-Vorschlag veröffentlicht. Der Hauptunterschied zwischen den beiden besteht darin, dass BRE dazu neigt, mehr Zeichen als Literale zu behandeln - ein "a" ist ein "a", aber auch ein "(" ist ein "(", kein Sonderzeichen) und beinhaltet somit mehr Backslashes geben Sie ihnen "spezielle" Bedeutung.

Die Diskussion der komplexen Unterschiede zwischen Vim und Perl auf einen separaten Kommentar hier ist nützlich, aber es ist auch erwähnenswert einige der einfacheren Möglichkeiten, in denen Vim regexes von der "akzeptierten" Norm abweichen . (., mit denen Sie wahrscheinlich Perl bedeuten) Wie oben erwähnt, sie meist in ihrer Verwendung eines vorhergehenden Backslash unterscheiden

Hier sind einige offensichtliche Beispiele:

 
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 

Das gibt Ihnen einen Eindruck der wichtigsten Unterschiede. Aber wenn Sie etwas komplizierter als die Grundlagen tun, schlage ich vor, dass Sie immer davon ausgehen, dass Vim-Regex wird von Perl-Regex oder Javascript-Regex und konsultieren etwas wie die Vim Regex website.

+0

Super, danke. Das stolpert mich jedes Mal auf! –

+0

das war die beste Antwort und half mir herauszufinden, genau das, was ich suchte, danke. – jimh

2

Versuchen Sie Vim's sehr magischen Regex-Modus. Es verhält sich mehr wie eine herkömmliche Regex, Sie können Ihr Muster einfach mit \v voranstellen. Weitere Informationen finden Sie unter :help /\v. Ich liebe es.

+0

von traditionellen Regex meinst du Basic Regular Expressions (BRE) oder PCRE? –

+1

pcre :) mehr Texttext – butterywombat

Verwandte Themen