Re: Matching of optional parts in regular expressions

Discussion in 'Python' started by Markus Elfring, Jul 8, 2004.

  1. Does this example show an error in the implementation?

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

    I think that the specified strings must not match to the pattern.
    How do you think about it?
    Markus Elfring, Jul 8, 2004
    #1
    1. Advertising

  2. In article <>,
    (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.
    Ulrich Schöbel, Jul 8, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    594
    Keith Thompson
    Mar 31, 2007
  2. Replies:
    1
    Views:
    944
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Apr 12, 2007
  3. kizk
    Replies:
    0
    Views:
    579
  4. Noman Shapiro
    Replies:
    0
    Views:
    232
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page