Why do you have to escape some metacharacters in their regex engine, but not others? For example:
/foo[1-9]*
works as expected, but the regular expression
foo[1-9]+
must be expressed as
/foo[1-9]\+
in vim. Anybody know?
A regex engine executes the regex one character at a time in left-to-right order. This input string itself is parsed one character at a time, in left-to-right order. Once a character is matched, it's said to be consumed from the input, and the engine moves to the next input character. The engine is by default greedy.
Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.
Using Regex in VimMany of Vim's search features allow you to use regular expressions. Some of them are: The search commands ( / and ? ) The global and substitute command-line (ex) commands ( :g and :s )
vi Regular Expressions (period) Matches any single character except a newline. Remember that spaces are treated as characters. Matches zero or more (as many as there are) of the single character that immediately precedes it.
This is because vim (actually vi) created their own regex flavor long before perl did. Even POSIX BRE and ERE came after vimwikipedia. They are still maintaining their own flavor so it's quite different.
To make the answer more resourceful here is a quote from ed
's wiki.
The editor was originally written in PDP-11/20 assembler in 1971 by Ken Thompson. Many features of ed came from the qed from his alma mater University of California at Berkeley3 Thompson was very familiar with qed, and had reimplemented it on the CTSS and Multics systems. His versions of qed were the first to implement regular expressions. Although regular expressions are part of ed, their implementation is considerably less general than that in qed.
Aspects of ed went on to influence ex, which in turn spawned vi. The non-interactive Unix command grep was inspired by a common special uses of qed and later ed, where the command g/re/p means globally search for the regular expression re and print the lines containing it. The Unix stream editor, sed implemented many of the scripting features of qed that were not supported by ed on Unix. In turn sed influenced the design of the programming language AWK - which inspired aspects of Perl.
These two paragraphs have a lot of information! I wish I could bold it all. Some highlights,
ed
in 1971. ed
was actually a reimplentation of qed
.qed
which is actually ed
.ed
, in 1976 William Joy (known as Bill Joy) wrote ex
wikipedia
vi
as the visual mode for a line editor called ex
wikipedia
grep
was inspired by special uses of qed
and later ed
.sed
was implemented as many of the scripting features of qed that were not supported by ed on Unixsed
influenced the design of awk
.So vi Regular Expression were in ed
which was written in 1971. It's long before any other regular expression flavor.
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