The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
Other - Regex validation
Discuss Regex validation in the Regex Programming forum on Dev Shed. Regex validation Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 14th, 2012, 01:35 PM
|
|
Registered User
|
|
Join Date: Feb 2012
Posts: 3
Time spent in forums: 14 m 13 sec
Reputation Power: 0
|
|
|
Other - Regex validation
trying to validate for numeric or exactly 'n/a'.
tried [0-9]|^(n\/a)$
but it passes 111 n/a. any suggestions. should
i pick a different profession?
|

February 14th, 2012, 02:07 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
Not sure if want to answer.
The ^$s need to be around the whole thing, otherwise it's just a normal "contains a"-type match.
|

February 14th, 2012, 02:13 PM
|
 |
CSS & JS/DOM Adept
|
|
Join Date: Jul 2004
Location: USA
|
|
Welcome to DevShed Forums, yoqi.
Since you're asking if you should give up at the same time you're asking the first question, then perhaps you should find something else to do since you don't seem very committed.
More seriously, programming, including regular expressions, takes time to learn.
Your expression isn't working because you're saying that a single digit appearing anywhere within the string is valid when you didn't intend to. You want the "^" at the very beginning and the "$" at the very end. You also want to use the "+" to allow one or more numerals. I suggest you use "\d", which is the shorthand for "[0-9]". The "?:" immediately inside the parentheses means that the group won't be a capturing group.
Now if you want to allow decimals and negative numbers as well, you can use this:
Code:
^(?:\-?\d*(?:\.\d+)?|n\/a)$
|

February 14th, 2012, 02:55 PM
|
 |
Turn left at the third duck
|
|
Join Date: Dec 2011
Location: Nelson, NZ
|
|
Hi yoqi,
requinix and Kravvitz have already answered beautifully.
I'd just like to explain this a bit more:
What happens when the regex "[0-9]|^(n\/a)$" tries to match the string "111 n/a"?
It matches the first 1. The "match" result does not mean that the match was the entire string. The engine reports "match" because it found a match. That match was the first 1.
The regex starts with [0-9]: match one digit. You feed it "111 n/a". Okay, the engine can match the initial 1. Then you say "OR..." (the alternation operator). So the engine is done.
As the others have explained, if you want to make sure the regex matches one thing and only that thing (e.g. the string "thing"), you need to say something like "^thing$". This means "match the beginning of the string, then the letter t, then the letter h, then the letter i, then the letter n, then the letter g, then the end of the string. So that regex would not match "things", because after the g the engine is not able to match the end of the string (specified by the $ anchor).
Wishing you all a fun day
|

February 14th, 2012, 05:39 PM
|
|
Registered User
|
|
Join Date: Feb 2012
Posts: 3
Time spent in forums: 14 m 13 sec
Reputation Power: 0
|
|
|
picking different profession
that was tongue in cheek. i'm actually a professional. i don't work with regex much and when i do, i can't seem to concentrate. it's like trying to read music at my age.
anywho, it works in php when i coded:
$rule = '#^(?:\d+|n\/a)$#';
if(isset($_POST['button'])){
$in = $_POST['in'];
if (preg_match($rule,$in)) {
echo 'good';
}else{
echo 'bad';
}
}
however, i need to place the regex in a column in a database to allow changes, but when i placed the #^(?:\d+|n\/a)$#
in a column in a database, the regex doesn't match. not sure why???
|

February 14th, 2012, 06:35 PM
|
 |
CSS & JS/DOM Adept
|
|
Join Date: Jul 2004
Location: USA
|
|
|
Last edited by Kravvitz : February 14th, 2012 at 06:37 PM.
|

February 14th, 2012, 07:06 PM
|
|
Registered User
|
|
Join Date: Feb 2012
Posts: 3
Time spent in forums: 14 m 13 sec
Reputation Power: 0
|
|
|
regex in database
doh! i had to escape the backslashes when inserting into the db.
#^(?:\\d+|n\\/a)$#
thanks for all your help.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|