in reply to Re: regex
in thread How do I detect if a number has a minus sign?

Thanks for your input. However it didn't help much. Let me give some more detail. I read -845, 4/3, and 1.25 into three variables $first, $second, $third. I want to be able to distinguish them: if ("it starts with a minus sign"){do something}; if ("it has / in it (fraction)") {do something}; if ("it contains three digits (may or may not start with minus sign)") {do something}; If it is too hard don't worry. Thanks anyways to all that replied.

Replies are listed 'Best First'.
Re^3: regex
by ikegami (Patriarch) on Sep 22, 2004 at 17:07 UTC

    List this?

    foreach (qw( -845 4/3 1.25 )) { /^[-+]?\d{3}$/ && do { print("$_ is a three digit integer, with a possible sign.\n"); #next; }; /^-/ && do { print("$_ starts with a minus sign\n"); #next; }; /\// && do { print("$_ has a / in it.\n"); #next; }; } __END__ output ====== -845 is a three digit integer, with a possible sign. -845 starts with a minus sign 4/3 has a / in it.

    Uncomment the nexts if you want numbers to only match one case.

Re^3: regex
by periapt (Hermit) on Sep 22, 2004 at 17:24 UTC
    Actually, you pretty much have it at this point. $sign, $num & $den contain the info you need. A series of decision statements should handle the rest (how you implement the decision tree is highly dependant on your purpose) so ...
    if($sign eq '-'){ do something ... } if(not $den){ do something .... } # $den will be undefined if no denom +inator is found if(length($num_ == 3){ do something ... }
    There are any number of ways you can identify/validate conditions on your term. (duff's reference has several). The idea is to peel off the parts of your test value that you need into seperate variables and then check the conditions individually. I have found that trying to find some super regex that does it all isn't worth the effort (although it can be aesthetically pleasing)


    PJ
    use strict; use warnings; use diagnostics; (if needed)