Matching of optional parts in regular expressions

Ulrich Schöbel usus at aladyn.de
Thu Jul 8 12:03:47 EDT 2004


In article <40ed1d8f.0407080602.bd6de25 at posting.google.com>,
	Markus.Elfring at web.de (Markus Elfring) writes:
> Does this example show an error in the implementation?

No. All three examples work absolutely correct.

> 
> % regexp {\s*(\d+)(?:%(\w+))?} {   !123!} z a b
> 1
> % foreach X {a b} {puts "$X=|[set $X]|"}
> a=|123|
> b=||

Regex is not anchored. It matches no whitespace (\s*),
three digits (\d+) and no word introduced by a % sign.

> % regexp {\s*(\d+)(?:%(\w+))?$} {   !123!} z a b
> 0

Same as before, but anchored at the end. The final ! is
not matched, so the entire regex doesn't match.

> % regexp {\s*(\d+)(?:%(\w+))?} {   456%} z a b
> 1
> % foreach X {a b} {puts "$X=|[set $X]|"}
> a=|456|
> b=||

Same as first example.

> 
> I think that the specified strings must not match to the pattern.
> How do you think about it?

Everything is working ok.

Best regards

Ulrich


-- 
For those of you who don't get this e-mail, let me know and I'll re-send it.



More information about the Python-list mailing list