in reply to Regex (counting) confusion :(

# Greedy $string = q(xxxx); $count = ($string =~ s/x*/#/g); print qq($string $count), $/; If you use "$count = ($string =~ s/x*/#/);" instead "$count = ($string =~ s/x*/#/g);" You will get what you want:)

Replies are listed 'Best First'.
Re: Re: Regex (counting) confusion :(
by Anonymous Monk on Sep 18, 2003 at 14:36 UTC
    greedy:
    "*" will matches "xxxx" and "" (count 2)
    but "+" only matches "xxxx" (count 1)

    not greedy
    "*" will matches "" "x" "" "x" "" "x" "" "x" "" (count 9)
    but "+" only matches "x" "x" "x" "x" (count 4)

    Don't ask me why:(