in reply to Regex trouble

You could use something like this:
$float='\d+(?:\.\d+)?'; $int='\d+'; @pats=("r($int)", "rect($float)x($float)", "dounut_s($int)x($int)"); $pat=join("|", @pats); @numbers=grep { defined($_) } ($str =~ /^(?:$pat)$/);
Add your MATCH patterns to @pats, enclosing in parenthesis the parts you want. The grep eliminates undef elements in the result of the match, which correspond to those patterns that did not match, and leaves only the matching parts.

--ZZamboni

Replies are listed 'Best First'.
RE: Re: Regex trouble
by cyclone (Novice) on May 17, 2000 at 00:47 UTC
    Would that $float regex match an integer? I will have to find the (?:\.\d+) example in my Perl books to see what it does. If it matches a non-float number then I think it will solve my problem.
      Yes it does:
      (?: - group, but don't generate back ref \. - a dot \d+ - followed by one or more digits )? - and the whole thing is optional
      So \d+(?:\.\d+)? matches a group of digits, optionally followed by a dot and more digits. So, it matches an integer or a float.

      If you want to allow things like "20." (no numbers after the dot) you would have it to \d+(?:\.\d*) (asterisk instead of plus sign after the second \d).

      --ZZamboni