[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] regex fun...



On Mon, Dec 13, 1999 at 10:54:59PM -0800, Nicholas G. Thornton wrote:
> /(\d*)d(.\d*)?(\(\d*\))?/ 
> I'm matching a string like "3d+2(5)" and parsing out to get {3,+2,5}. 
> The "3d" has to be there the "+2" will most likely be there but can be 
> left out. it could also be something like "-2". the "(5)" is also 
> optional. 
>      But basically what the problem is, is that with the above regexp 
> "3d(5)" will give me {3,(5,} instead of {3,,5}. I'm wondering how I can 
> give $3 matching presedance over $2? Any other bright ideas that may 
> help will of course be accepted :) 
> 

"3d(5)" returns ('3', '(5') because the period in your regexp matches the
left parenthesis.  If you want that period to only match + or -, you should
change it to [+-].

Here's the regex I'd use:

/(\d+)d([+-]\d+)?(?:\((\d+)\))?/;

/(\d+)              # required number
 d                  # d
 ([+-]\d+)?         # an optional modifier
 (?:\((\d+)\))?     # an optional parenthesized number
/x;

Ronald

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org