in reply to Is there any other way to avoid greedy matching

David.. My basic idea is to see if the string has exactly two digits or not. One solution which i thought is if(^\d\d$); But is there any other way??
  • Comment on Re: Is there any other way to avoid greedy matching

Replies are listed 'Best First'.
Re^2: Is there any other way to avoid greedy matching
by ikegami (Patriarch) on Sep 24, 2007 at 04:35 UTC

    /^\d\d$/ will also allow a trailing newline.
    /^\d\d\z/ checks if the string contains exactly two digits and nothing else

    Yes, there are other ways, but they are more complicated.

Re^2: Is there any other way to avoid greedy matching
by erroneousBollock (Curate) on Sep 24, 2007 at 03:48 UTC
    If you've already verified that the string contains no non-digit characters, it's a simple as
      /^\d{2}$/

    Otherwise, something like this might work:
      scalar(() = $foo =~ /(\d)/g) == 2

    -David

    Update: Wow, what was I smoking?