Re: Regular expression: How to determine wether entry is a number?



On Tue, Jul 29, 2008 at 05:46:09AM -0700, Jan-Henrik wrote:

Now I would like to check wether the user really entered a number and
not letters. What would a check like that look like? A regular
expression like this:
----------------------------------------
unless ($foo =~ /[a-zA-Z\D+][^.][\D*]/ {...};
----------------------------------------

Is there an easier or more beautiful way?

perldoc -q scalar number

Also, how would I substract just a number from a string? Searched the
net for an example but didn't succeed, so sorry for asking a question
like that...

In general you might not have to, you can probably just use it as a number and
perl will do the right thing. But otherwise you could just put parentheses
around your regex to capture the numeric part.

--
Paul Johnson - paul@xxxxxxxx
http://www.pjcj.net
.



Relevant Pages

  • Re: Frustration - split not working as expected
    ... Read "perldoc perlrequick", ... get you up to speed quickly regarding regular expressions. ... There's no real reason for you to use "readline" here - much better ...
    (perl.beginners)
  • Re: need a simple image builder
    ... On Jun 18, 11:56 am, Martin Leese ... Images depict gray values on a regular ... rectangular polygon window of sides W and H units. ... and color them indexed on the scalar attribute. ...
    (sci.image.processing)
  • Re: List Util insert_at(index, value), delete_at(index)
    ... I looked a the perldoc mentioned and saw the following: ... there's no such thing as a list in scalar ... you're using the comma operator in scalar context, ...
    (comp.lang.perl.misc)
  • Re: how to flush a file
    ... that tail -f on the the log file shows the progress on a regular ... basis. ... This is from `perldoc -f select`: ... Some programmers may prefer to think of filehandles as objects ...
    (comp.lang.perl.misc)
  • Re: Why Camel is Better than Perldoc
    ... > tuxy> perldoc regex ... > tuxy> perldoc perldoc regular ... <ten screens of info about regular expressions snipped> ... deal at length with regexes. ...
    (comp.lang.perl.misc)