9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: "Russ Cox" <rsc@swtch.com>
To: 9fans@cse.psu.edu
Subject: Re: [9fans] regexp to match paragraphs in troff documents
Date: Thu,  7 Jun 2007 13:46:15 -0400	[thread overview]
Message-ID: <20070607174226.379901E8C1C@holo.morphisms.net> (raw)
In-Reply-To: <082ffdbcfda12b51637105e95ea479c8@mteege.de>

matthias:
> I've tried to pipe paragraphs of a troff document to fmt but I
> have problems with the correct regular expression. My first attempt
> "/^\.[A-Z]++.*\n(^[^]*)*\n^\.[A-Z]++.*\n/" matches only any second
> paragraph because the expression is overlapping. Does anyone have a nice
> idea to match troff parapgraphs?

It looks like you'd be happy with

	,y/^\..*\n/ |fmt

or, avoiding fmt of zero-length ranges (just a little faster)

	,y/(^\..*\n)+/ |fmt

rog:
> for other cases, i suppose it might be nice to have non-greedy matching,
> in which case you could do something like:
> ,x/^\.[A-Z][A-Z].*\n(.*\n)*?\.[A-Z][A-Z]\n/
> russ: how easy do you think it would be to put non-greedy matching into
> the acme/sam regexp engine?

it's trivial but it doesn't make sense.

in plan 9 regular expressions (as in awk), the semantics
are that the leftmost longest overall match is chosen,
even if that means not repeating a * as much as possible.

for example, consider /a*(ab)?/ against "aab".
perl will match "aa" because the a* greedily grabs "aa"
leaving (ab)? no choice but to match the empty string.
plan 9 will match "aab" because that is a longer match:
the a* selflessly matches less so that the overall
expression can match more.

since plan 9 doesn't have the greedy-like-perl * operator,
it doesn't make sense to think about adding a
non-greedy-like-perl * operator.

the y iterator handles about 90% of the reasons people use
non-greedy operators, so i'm happy to leave things as is.

russ



      parent reply	other threads:[~2007-06-07 17:46 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-06-07 14:28 Matthias Teege
2007-06-07 14:50 ` rog
2007-06-07 17:46 ` Russ Cox [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070607174226.379901E8C1C@holo.morphisms.net \
    --to=rsc@swtch.com \
    --cc=9fans@cse.psu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).