Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

need help with a Regex

by Peamasii (Sexton)
on Oct 06, 2004 at 14:44 UTC ( [id://396999]=perlquestion: print w/replies, xml ) Need Help??

Peamasii has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking for a regexp to match any of the following:
- empty string
- the character ? alone
- a number
and not to match any other strings.

Thanks a lot for helping this novice.

Replies are listed 'Best First'.
Re: need help with a Regex
by dragonchild (Archbishop) on Oct 06, 2004 at 14:49 UTC
    What have you tried? How has it failed? What documentation have you read to help yourself out? We don't provide answers off the bat. We prefer that you show us what you've tried before asking for help. That way, we can address the issues you're having directly, rather than spoon-feeding you.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: need help with a Regex
by Limbic~Region (Chancellor) on Oct 06, 2004 at 14:51 UTC
    Peamasii,
    I am terrible at regexes myself, but it seems like an anchored character class with an ungreedy quantifier would do the trick.
    /^[\d?]?$/

    Cheers - L~R

    After reading dragonchild's reply, I have to agree - reading perldoc perlre, checking the Categorized Questions and Answers, and Super Searching are always good places to start. It is like the saying goes Give a man a fire and he stays warm for the day, set a man on fire and he is warm for the rest of his life give a man a fish and he is full for a day, teach a man to fish and he never goes hungry.
      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
        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

        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
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://396999]
Approved by dragonchild
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2024-03-28 18:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found