Dogg has asked for the wisdom of the Perl Monks concerning the following question:

Damn!

I should be able to figure this out, but I've been struggling.

I've got a string ($single = "12"). I want to match everything from a list (@allbonds) containing either:
12 then a single digit (like 123) or
12 then a , and 2 digits (like 12,34).

I'm having trouble excluding cases I don't want. So far I've had trouble getting either one to work (since I need the , to act as a boundry). I'm not against doing it in two separate steps (first matching /$single\d[^,12345]/ and then matching /$single,\d\d/but it's still giving me too much.

@allbonds looks like (12 13 123 12,45 123,45 12,345 1234 12345) and I should pick 123 and 12,45 only.

Damn.
Thanks

Replies are listed 'Best First'.
Re: Pattern matching
by mdillon (Priest) on Mar 26, 2002 at 00:28 UTC
    Try this:
    $single = "12"; @allbonds = qw(12 13 123 12,45 123,45 12,345 1234 12345); @somebonds = grep /^$single(?:\d|,\d{2})$/o, @allbonds;
      Thanks. That works perfectly.
      I've never used grep (I've always made do with simpe matching).
      This will make me learn it.

      Thanks again.

        You're missing the point. It's not that he's using grep, but that he's anchored the match with a $ at the end of the regexp that's important. You were matching more than you thought you should be because you hadn't specified that the regexp had to match the entire string.