Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: need help with a Regex

by Dietz (Curate)
on Oct 06, 2004 at 15:32 UTC ( #397027=note: print w/replies, xml ) Need Help??


in reply to Re: need help with a Regex
in thread need help with a Regex

L~R, you are right with using anchors to limit the scope.
But your solution would only match a single digit instead of a number (not what the OP wanted).

One solution would be (not documented as of dragonchild's post):
/^(\d+|\?)?$/

++ for the right approach
Monkish Greetings,
Dietz

Replies are listed 'Best First'.
Re^3: need help with a Regex
by Limbic~Region (Chancellor) on Oct 06, 2004 at 15:44 UTC
    Dietz,
    But your solution would only match a single digit instead of a number (not what the OP wanted). emphasis mine

    Well see here is the quandry. You have interpreted it one way and I have another. Peamasii didn't define what exactly s/he meant by number. It could mean a single digit as I used, it could mean an integer which you described (which will match 0004 and miss -4 btw), or it could mean any number (pun intended) of things. For instance:

    use Scalar::Util 'looks_like_number'; if ( looks_like_number( $foo ) && $foo =~ /^\??$/ ) {...}
    It is probably as common a mistake to assume what you are thinking is universally understood when asking questions as it is for the person that is answering to assume they know what you mean. I think we both fall into the latter category this time.

    Cheers - L~R

      L~R, you are right again.
      Didn't see it the way you described, especially when it comes to numbers with a minus before.

      I was just recognizing the single digit match. And I was hesitating to post anyways (as of dragonchild's post above)

      And I really didn't mean to offend you, I have great respect for you - as I have for most other monks here.

      Respect, Dietz
        Dietz,
        I wasn't offended and I hope I wasn't offending. I saw it as an opportunity to not argue over what someone might have meant and instead point out how assumptions lead down the wrong road.

        Cheers - L~R

      Well, numbers fall into so many possible formats that it's impossible to catch them all. I'm going to assume that any integer or decimal format is allowed, and write the following:
      foreach (<DATA>) { print $_ if $_ =~ /^(\?|-?\d*\.?\d+)?$/; } __DATA__ ? 3 3.3 .3 -3 -3.3 -.3 ? 3-3 -
        TedPride,
        That is why I suggested Scalar::Util's 'looks_like_number'. For instance, your regex misses at least numbers in scientific notation. Here is the applicable code:
        # Copied from Scalar::Util sub looks_like_number { local $_ = shift; # checks from perlfaq4 return $] < 5.009002 unless defined; return 1 if (/^[+-]?\d+$/); # is a +/- integer return 1 if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/); # +a C float return 1 if ($] >= 5.008 and /^(Inf(inity)?|NaN)$/i) or ($] >= 5.006 +001 and /^Inf$/i); 0; }

        Cheers - L~R

Re^3: need help with a Regex
by Eimi Metamorphoumai (Deacon) on Oct 06, 2004 at 15:45 UTC
    You probably don't need the capturing parens. I'd write it as
    /^(?:\d*|\?)$/
    where the empty string is just a zero digit number, but that's just me.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://397027]
help
Chatterbox?
[ELISHEVA]: the data source, or one of them, is the OECD - they provide a *lot* of data that ought to be easily available to perl programmers.
[erix]: it might be cunning to mention the module in the title... :)
[ELISHEVA]: fancy that - a title that actually describes the problem :-)
[ELISHEVA]: but actually thanks for the reminder
[Discipulus]: DBI::CSV + utf8 = BOO?M
[erix]: in extremis we tend to forget stuff ;)
[ELISHEVA]: \Disciplus : lol
[Discipulus]: and ELISHEVA we waait one your post since ~2years... ;=)
[ELISHEVA]: has it really been that long?
[Discipulus]: it seems..

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2017-05-28 20:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?