I noticed that vim's substitute regex is a bit different from other regexp. What's the difference between them?
vim has a mixed up regex syntax.
There are also two types of regular expressions: the "Basic" regular expression, and the "extended" regular expression.
Regex isn't suited to parse HTML because HTML isn't a regular language. Regex probably won't be the tool to reach for when parsing source code. There are better tools to create tokenized outputs. I would avoid parsing a URL's path and query parameters with regex.
i) makes the regex case insensitive. (? s) for "single line mode" makes the dot match all characters, including line breaks.
"Regular expression" really defines algorithms, not a syntax. What that means is that different flavours of regular expressions will use different characters to mean the same thing; or they'll prefix some special characters with backslashes where others don't. They'll typically still work in the same way.
Once upon a time, POSIX defined the Basic Regular Expression syntax (BRE), which Vim largely follows. Very soon afterwards, an Extended Regular Expression (ERE) syntax proposal was also released. The main difference between the two is that BRE tends to treat more characters as literals - an "a" is an "a", but also a "(" is a "(", not a special character - and so involves more backslashes to give them "special" meaning.
The discussion of complex differences between Vim and Perl on a separate comment here is useful, but it's also worth mentioning a few of the simpler ways in which Vim regexes differ from the "accepted" norm (by which you probably mean Perl.) As mentioned above, they mostly differ in their use of a preceding backslash.
Here are some obvious examples:
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
That gives you a flavour of the most important differences. But if you're doing anything more complicated than the basics, I suggest you always assume that Vim-regex is going to be different from Perl-regex or Javascript-regex and consult something like the Vim Regex website.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With