Regular Expression Problem
Posted by keith (keith), 1 March 2005Hi,
I have a problem with this regular expression script and cannot figure out why it doesn't work.
The code is supposed to just check that only numbers, brackets'()', spaces or the plus'+' sign are allowed.
eg +44 (0) 118 9123456
However it seems to allow anything!
If I change the expression to "([0-9 \(\)\+]+)". Then it works (but at least one character must be given).
I want the fax number to be optional but validated if entered.
What's wrong with this?
I put it into both 'Regex Coach' and 'Regular Expression Editor' software and both seem to think it's fine.
Why is the change from '+' to '*' causing an issue?
Posted by admin (Graham Ellis), 1 March 2005"+" means ONE or more occurrence of the element preceeding, and "* means ZERO or more occurrences. If your entire regular expression has an * after it, then it will always match as every possible input string contains nothing. That's a tough one to work even in English.
You probably want to use anchors (assertions) to say that your regular expression starts with ... and ends with ... rather that the default which is "contains". Try:
Posted by keith (keith), 1 March 2005That's cracked it.
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: firstname.lastname@example.org • WEB: http://www.wellho.net • SKYPE: wellho