Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 14th, 2012, 01:35 PM
yoqi yoqi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 3 yoqi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #2  
Old February 14th, 2012, 02:07 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,711 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 6 h 48 m 5 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
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.
Code:
#^(\d+|n/a)$#

Reply With Quote
  #3  
Old February 14th, 2012, 02:13 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,835 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 22 h 40 sec
Reputation Power: 4192
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.
Code:
^(?:\d+|n\/a)$

Now if you want to allow decimals and negative numbers as well, you can use this:
Code:
^(?:\-?\d*(?:\.\d+)?|n\/a)$
__________________
Spreading knowledge, one newbie at a time. I'm available for hire at Dynamic Site Solutions.

Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

Remember people spend most of their time on other people's sites (so don't violate web design conventions).

Reply With Quote
  #4  
Old February 14th, 2012, 02:55 PM
ragax's Avatar
ragax ragax is offline
Turn left at the third duck
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Location: Nelson, NZ
Posts: 93 ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 24 m 37 sec
Reputation Power: 92
Hi yoqi,

requinix and Kravvitz have already answered beautifully.

I'd just like to explain this a bit more:
Code:
it passes 111 n/a


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
__________________
Regex Tutorial | Latest RegexBuddy Demo

Reply With Quote
  #5  
Old February 14th, 2012, 05:39 PM
yoqi yoqi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 3 yoqi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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???

Reply With Quote
  #6  
Old February 14th, 2012, 06:35 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,835 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 22 h 40 sec
Reputation Power: 4192
You probably need to use stripslashes: http://us2.php.net/manual/en/function.stripslashes.php

I'm finding that reading threads in this forum is a good way to learn more about regex.

P.S. It would be helpful if you put your code between [code][/code] tags or [php][/php] tags in the future.

Last edited by Kravvitz : February 14th, 2012 at 06:37 PM.

Reply With Quote
  #7  
Old February 14th, 2012, 07:06 PM
yoqi yoqi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 3 yoqi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Other - Regex validation

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap