regular expression

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

i intend to validate a string of numbers:

The conditions are: it can start with a + sign or without. but all must be  
in numbers. a space is allowed

Consider the following numbers



The above 2 numbers are valid. I've tried the following regexp:


It doenst work as intended.


goes through!


1234567helloworld doesn't!

please advise

 Posted Via Premium Usenet Newsgroup Services

Re: regular expression

Eric Layman wrote:

Quoted text here. Click to load it

/^\+?[0-9 ]+$/

Toby A Inkster BSc (Hons) ARCS
Contact Me ~
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!

Re: regular expression

What your original regular expression is saying is that the last
character in the string is a + or a number 0 thru 9.

/^\+?[0-9 ]+$/

What Toby's expression is saying is that it starts with an optional +
(? means 0 or 1) and then has 1 or more #'s or spaces in a row until
the end of the string.

The ^ means starting at the beginning of the string and the $ means
the end.

Hope that helps explain it somewhat.

Site Timeline