[Tutor] non-greedy matching
A.T.Hofkamp
a.t.hofkamp at tue.nl
Fri Jan 30 13:44:42 CET 2009
spir wrote:
> Hello,
>
> imagine you need to match such a pattern:
>
> pat : (@@ [charset]* @@) | [charset]*
> ... where [charset] has to include '@'
>
> My questions are:
> * Is there any other way than using a non-greedy form of [charset]* ?
Something like this?
(in pseudo-RE syntax)
"(@@" ( [^@]* "@" [^@] | [^@]* "@" "@"+ [^@)] )* [^@]* "@" "@"+ ")"
to understand, break the above down in pieces:
"(@@" ( A | B )* C
with A: [^@]* "@" [^@]
# lots of chars, then a @, and a non-@
B: [^@]* "@" "@"+ [^@)]
# lots of chars then at least two @, and a non-closing bracket
# (the non-@ at the end is for forcing all @ to be matched in "@"+)
C: [^@]* "@" "@"+ ")"
# lots of chars, then at least two @, and finally a closing bracket
> * How, actually, is non-greedy character string matching performed?
That's what I'd like to know too.
(and while we are at it, what about the \b )?
Sincerely,
Albert
More information about the Tutor
mailing list