Thursday, August 16, 2012

Re: Pattern matching question

On 16/08/12 12:31, Erik Christiansen wrote:
> On 16.08.12 03:15, Tony Mechelynck wrote:
>> Oh, regular expressions work quite nicely, but AFAICT each of the
>> Unix programs which uses them has an almost, but not quite, identical
>> grammar for them. So they look familiar, but at some unexpected times
>> the same regexp does just very slightly different things in different
>> programs: IOW you have to check each time how regexps works for the
>> software you are using _now_.
>
> Sadly yes, if we let that happen. One way to avoid that irritation is to
> avoid using awk AND sed AND perl, etc. Sticking to one of the two major
> classes, Extended REs (EREs), or the obsolete Basic REs (BREs), is a
> ripper of a start, I find. (e.g. use only egrep or "grep -E", and only
> awk. There are then no differences.)
>
> Interestingly:
>
> $ man 7 regex # seems to think we are moving forward:
>
> »Regular expressions ("RE"s), as defined in POSIX.2, come in two forms: modern
> REs (roughly those of egrep; POSIX.2 calls these "extended" REs) and obsolete
> REs (roughly those of ed(1); POSIX.2 "basic" REs). Obsolete REs mostly
> exist for backward compatibility in some old programs; they will be discussed
> at the end. POSIX.2 leaves some aspects of RE syntax and semantics open;
> "(!)" marks decisions on these aspects that may not be fully portable to
> other POSIX.2 implementations.«
>
> Vim's bewildering variety of regex flavours must have its varied uses.
> The "\v" variation is the only one worth using, I submit, because it
> is near as dammit POSIX ERE. (At least close enough that I don't
> remember any big surprises.) Now our text searcher, text processor, and
> editor all use EREs. :-))

I beg to differ. This would mean always using "very magic" regexps. I
always leave the 'magic' setting at its default (magic) and never change
it in a pattern, because that is what is assumed at most places in the
Vim help where patterns are discussed.

Until now, I haven't felt the need for other than very simple regexps in
programs other than Vim — let's say ^ $ \< \> and literal-character
matching (maybe also . * \. \* \/ and \\), and these seem to work in
most of what I need, namely less (including the pager for man) and, I
think, grep. I'll be prudent and try to find the right documentation
before I use more complex pattern matching outside Vim (whose help still
amazes me every day by its completeness, now that I have "domesticated"
the various ways of searching it, as detailed in the helphelp.txt
helpfile, which didn't exist when I came to Vim a few years ago).

>
> But why are EREs better than BREs? Because they greatly reduce the
> painful obfuscation of backslash snowstorms which occur in BREs,
> allowing us to read them without counting on fingers and toes every time
> we hit backslashes.
>
> I tried building Vim with a POSIX regex library once. The resulting
> behaviour was a dream, except for the :help. It broke. :-(
>
> Erik
>
> P.S. OK, I can't remember what regex flavour Expect uses, but it is
> nearly a decade since I last used it.
>


Best regards,
Tony.
--
WOMAN: I didn't know we had a king. I thought we were an autonomous
collective.
DENNIS: You're fooling yourself. We're living in a dictatorship. A
self-perpetuating autocracy in which the working classes--
WOMAN: Oh there you go, bringing class into it again.
DENNIS: That's what it's all about if only people would--
The Quest for the Holy Grail (Monty
Python)

--
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

No comments: