----- Original Message -----
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, April 24, 2001 6:34 PM
Subject: Complex Regex.


> The Text has numbers in it apparently-1.0 at random but 12 actually not...
> $% ---- 12.3   0.9  .333  33 and -33.9000009  I need to extract
> u&t(y2335.09u see
>
> all the legiti5.33mate integers and floating point numbers and separate
them
> with CRs.  All text and nun-numeric data should be chucked.  it needs to
> look for negative numbers but 2-1 and 4-66.7  are "2  1  4  66.7".   That
is
> - is a delimiter unless there's a space in front in which case the number
is
> negative.
>
> The output for the text above should be:
>
> -1.0
> 12
> 12.3
> 0.9
> .333
> 33
> -33.9000009
> 2335.09
> 5.33
> 2
> 1
> 4
> 66.7
> 2
> 1
> 2              <----- this should be a 4 :-)
> 66.7

I would not try to match all number in 1 expression.
instead you could look for parts that match your criteria and then search
the remainder if the string.
Below is a solution to do this:

while( $text =~ s/(\s+-)?(\d+)(\.\d+)?(.*)//s) {
   print "$1$2$3\n";
   $text = $4||$3;
}

The expression has these parts:

(\s+-)? : optional a whitespace followed by a minus sign
(\d+)   : obligatory one or more digits.
(\.\d+) : optional a dot followed by one or more numbers
(.*)     : the rest of the string.

All expressions within brackets will be put in $1 trough $4.
The second sub-expression  will be $2 or $1 (in case there isnt a minus
sign).
So the rest of the string will be $4 or $3.

Maarten.

Reply via email to