9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] regexp to match paragraphs in troff documents
@ 2007-06-07 14:28 Matthias Teege
  2007-06-07 14:50 ` rog
  2007-06-07 17:46 ` Russ Cox
  0 siblings, 2 replies; 3+ messages in thread
From: Matthias Teege @ 2007-06-07 14:28 UTC (permalink / raw)
  To: 9fans

Moin,

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?

Many thanks
Matthias



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [9fans] regexp to match paragraphs in troff documents
  2007-06-07 14:28 [9fans] regexp to match paragraphs in troff documents Matthias Teege
@ 2007-06-07 14:50 ` rog
  2007-06-07 17:46 ` Russ Cox
  1 sibling, 0 replies; 3+ messages in thread
From: rog @ 2007-06-07 14:50 UTC (permalink / raw)
  To: 9fans

> 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's not easy (maybe not possible?) to do what you're doing unless you
can unambiguously find the start and end of paragraphs (e.g. between ^\.PP and ^$),
in which case you can use something like:

,x/^\.PP/.,/^$/|fmt

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?

PS. ++ means exactly the same as +


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [9fans] regexp to match paragraphs in troff documents
  2007-06-07 14:28 [9fans] regexp to match paragraphs in troff documents Matthias Teege
  2007-06-07 14:50 ` rog
@ 2007-06-07 17:46 ` Russ Cox
  1 sibling, 0 replies; 3+ messages in thread
From: Russ Cox @ 2007-06-07 17:46 UTC (permalink / raw)
  To: 9fans

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



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-06-07 17:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-06-07 14:28 [9fans] regexp to match paragraphs in troff documents Matthias Teege
2007-06-07 14:50 ` rog
2007-06-07 17:46 ` Russ Cox

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