Saturday, December 14, 2013

Re: Regular expression and VIm

On 13.12.13 08:23, Tim Chase wrote:
> On 2013-12-13 19:35, vicky b wrote:
> > I see most of time for complex editng regular expression is a
> > must , so what is the best way to start learning regular expression
> > any thing specific to vim.
>
> There are a number of good resources for learning regular
> expressions, as a quick web search for "learn regular expressions"
> returns. Also searching your favorite book-seller's site for
> "regular expression" returns a number of well-known titles.

Tim's advice is very good. (Though some very close to hand info is
mentioned below) Everyone and his dog have created dialects of
regex, and moving between them is painful and inefficient, so
specialising in one which is widely portable is perhaps just as
important as finding one that is efficient. Best is to have both,
naturally.

The only regex book I have to hand is O'Reilly's "Mastering Regular
Expressions", and it looks in some detail at variations from Awk to
Perl, going via Emacs. (But it doesn't seem to mention Vim, so you
might prefer to spend your money on something else.)

Perhaps the cheapest and quickest way to grab an intro on regexes is
undoubtably to type:

$ man 7 regex

The language is terse, and information dense, so retreat to a book is no
sign of a faint heart. (Later, it's a basic (incomplete) quick
reference.) It does, however, highlight two worthy considerations:

1) POSIX compliant regexes: These are portable across POSIX compliant
utilities, thus saving a lot of time and brain pain unlearning and
relearning.

2) There are two major classes of regex: EREs (Extended Regular
Expressions), which are more powerful and efficient than;
BREs (Basic regular Expressions), which are obsolete (see the man 7
manpage) and often require so many backslashes that they become
unreadable.

Vim is blessed with one regex mode which pretty much offers both the
portability and the greater power of POSIX EREs: \v
If you use that, then awk, mutt, egrep (or grep -E), sed -r, vim with
the \v prefix, lex (OK, it also has extensions), and other *nix tools
are your oyster - without a mental hernia.

There is also a treatment of regexes in: $ man grep
It reiterates that " basic regular expressions are less powerful",
and briefly describes \> (see below) and good stuff like intervals: {n,m}
It also succinctly shows why BREs quickly become unreadable due to
backslash prolixity:
»
Basic vs Extended Regular Expressions
In basic regular expressions the meta-characters ?, +, {, |, (, and )
lose their special meaning; instead use the backslashed versions
\?, \+, \{, \|, \(, and \).
«

For comparison, one can try: $ man pcre
There's a _lot_ of overlap with POSIX EREs, and some extensions.

...
> So Perl/Python regexps have "\b" for indicating a word-boundary while
> Vim splits them out into "\<" and "\>" for beginning-of-word and
> end-of-word (I think Perl is adding this functionality in an upcoming
> or recent release).

That is a welcome step toward standardisation, not least because \<
is an extension beyond POSIX, unless I'm out of date. But quite a few
unix utilities use \<, so it's a delight for it to become more portable.

> For the basics, it's mostly a matter of whether something is escaped
> with a backslash or not (grouping with parens, the one-or-more "+",
> the N-M curly-braces, etc). For more complex things like
> back-references and zero-width assertions, the syntax varies a bit
> more widely.

Please forgive reiteration that we can choose whether to suffer the
indignity of obsolete backslash burdens, by using ERE (Vim: \v) instead.
(Should BRE possibly stand for: Backslashing is Really Excessive here? )

> So first learn the basics of regular expressions, then read up (or ask
> questions here on the list) on Vim-specific flavor.

+1

Another cheap reference is

http://en.wikipedia.org/wiki/Regular_expression#POSIX_basic_and_extended

Erik

--
The wonderful thing about standards is that there are so many of them.
- Andy Tanenbaum

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: