Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: regex

by jbware (Chaplain)
on Sep 22, 2004 at 16:13 UTC ( [id://392953]=note: print w/replies, xml ) Need Help??


in reply to How do I detect if a number has a minus sign?

This should probably get you started.
use strict; while (<DATA>) { #OLDmy ($sign, $num, $den) = m@(-)?(\d+)/?(\d*)@; my ($sign, $num, $den) = m@(-)?(\d+)(?:/(\d+))?@; print "sign:$sign numerator:$num denominator:$den\n"; } __DATA__ 23 -35 -23/43 2/3 5/

-jbWare

Update:Fixed regex based on ikegami's suggestion. Also pulled / out of the denominator in ikegami's regex.

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

    slight change:
    m@(-)?(\d+)(?:(/\d+)?)@;
    Yours matched "5/".

Re^2: regex
by algonquin (Sexton) on Sep 22, 2004 at 16:47 UTC
    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.

      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.

      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)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://392953]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-19 04:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found