cavemong Posted December 30, 2008 Share Posted December 30, 2008 I currently have a reg expression set up so that a form field can include letters numbers and not be over 50 characters. I have it set up like so: $adreg = '/^[a-zA-Z0-9]{1,50}$/'; However if I were to type "12 smith street", it says its not valid. What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/138921-solved-regular-expression-error/ Share on other sites More sharing options...
Maq Posted December 30, 2008 Share Posted December 30, 2008 Have you tried The Regex Section? Quote Link to comment https://forums.phpfreaks.com/topic/138921-solved-regular-expression-error/#findComment-726502 Share on other sites More sharing options...
.josh Posted December 30, 2008 Share Posted December 30, 2008 you aren't allowing for spaces in your class. do $adreg = '/^[a-zA-Z0-9 ]{1,50}$/'; or $adreg = '/^[a-zA-Z0-9\s]{1,50}$/'; Quote Link to comment https://forums.phpfreaks.com/topic/138921-solved-regular-expression-error/#findComment-726508 Share on other sites More sharing options...
cavemong Posted December 30, 2008 Author Share Posted December 30, 2008 Have you tried The Regex Section? Sorry man I didn't see that. you aren't allowing for spaces in your class. do $adreg = '/^[a-zA-Z0-9 ]{1,50}$/'; or $adreg = '/^[a-zA-Z0-9\s]{1,50}$/'; Thanks that worked. Quote Link to comment https://forums.phpfreaks.com/topic/138921-solved-regular-expression-error/#findComment-726515 Share on other sites More sharing options...
.josh Posted December 30, 2008 Share Posted December 30, 2008 fyi putting a ' ' matches a single space, \s matches a space, tab or linebreak. Quote Link to comment https://forums.phpfreaks.com/topic/138921-solved-regular-expression-error/#findComment-726521 Share on other sites More sharing options...
nrg_alpha Posted December 31, 2008 Share Posted December 31, 2008 Yet another alternative: $adreg = '/^[a-z\d\x20]{1,50}$/i'; Quote Link to comment https://forums.phpfreaks.com/topic/138921-solved-regular-expression-error/#findComment-726679 Share on other sites More sharing options...
.josh Posted December 31, 2008 Share Posted December 31, 2008 $adreg = '/^[\w ]{1,50}$/'; win! Quote Link to comment https://forums.phpfreaks.com/topic/138921-solved-regular-expression-error/#findComment-726693 Share on other sites More sharing options...
nrg_alpha Posted December 31, 2008 Share Posted December 31, 2008 $adreg = '/^[\w ]{1,50}$/'; win! Not if the user doesn't want underscores EDIT - And in this case, I would re-write my version to use [0-9] instead of \d as \d can match more than 0-9 Quote Link to comment https://forums.phpfreaks.com/topic/138921-solved-regular-expression-error/#findComment-726698 Share on other sites More sharing options...
.josh Posted December 31, 2008 Share Posted December 31, 2008 Touché Quote Link to comment https://forums.phpfreaks.com/topic/138921-solved-regular-expression-error/#findComment-726700 Share on other sites More sharing options...
nrg_alpha Posted December 31, 2008 Share Posted December 31, 2008 And on the note of \w and what it encompasses, I am simply baffled as to why PCRE doesn't have a short-hand character class that ONLY matches a-zA-Z0-9 In other words, simply the alphabet and numbers. I really find that inclusion of the underscore annoying as hell.. Quote Link to comment https://forums.phpfreaks.com/topic/138921-solved-regular-expression-error/#findComment-726702 Share on other sites More sharing options...
.josh Posted December 31, 2008 Share Posted December 31, 2008 ah! read your book, I know you got one. It's leftovers from old school times when regex was originally thought to be most useful for sorting through code. The _ used to be part of variable naming convention back in the day. edit: Though I do agree, should have a shorthand for 'real' word words. Quote Link to comment https://forums.phpfreaks.com/topic/138921-solved-regular-expression-error/#findComment-726706 Share on other sites More sharing options...
nrg_alpha Posted December 31, 2008 Share Posted December 31, 2008 ah! read your book, I know you got one. It's leftovers from old school times when regex was originally thought to be most useful for sorting through code. The _ used to be part of variable naming convention back in the day. edit: Though I do agree, should have a shorthand for 'real' word words. Touché back @ u... But yeah, ok.. make a NEW shorthand character class without the underscore.. and a new character class aside form \d that doesn't match exponents (just pure 0-9). CV, I can only shake my head and ask why lord?...why? Quote Link to comment https://forums.phpfreaks.com/topic/138921-solved-regular-expression-error/#findComment-726708 Share on other sites More sharing options...
.josh Posted December 31, 2008 Share Posted December 31, 2008 eh. I can understand reducing a-zA-Z0-9 to something shorthand but 0-9 would just be reduced 1 character. Don't really see the point. Quote Link to comment https://forums.phpfreaks.com/topic/138921-solved-regular-expression-error/#findComment-726712 Share on other sites More sharing options...
nrg_alpha Posted December 31, 2008 Share Posted December 31, 2008 eh. I can understand reducing a-zA-Z0-9 to something shorthand but 0-9 would just be reduced 1 character. Don't really see the point. Well, it would reduce two for sure (²³). Point being, just having pure 0-9 would be nice (and not have to worry about extras 'slipping through the cracks'). EDIT - I misunderstood.. you mean total characters 0-9 as compared to \d. Sorry.. I understand now. Quote Link to comment https://forums.phpfreaks.com/topic/138921-solved-regular-expression-error/#findComment-726714 Share on other sites More sharing options...
.josh Posted December 31, 2008 Share Posted December 31, 2008 yeah, meant 0-9 = 3 chars vs. \d = 2 chars don't really see the point, unless they were to say, make the current \d mean that, and make the current \d something else (so that people can logically know that \d means digit) Quote Link to comment https://forums.phpfreaks.com/topic/138921-solved-regular-expression-error/#findComment-726722 Share on other sites More sharing options...
nrg_alpha Posted December 31, 2008 Share Posted December 31, 2008 yeah, meant 0-9 = 3 chars vs. \d = 2 chars don't really see the point, unless they were to say, make the current \d mean that, and make the current \d something else (so that people can logically know that \d means digit) Yeah. I wasn't so concerned about the reduction in key strokes.. I'm more concerned with what those shorthand character classes actually match. So in the case of \d, I don't like that it can match: $str = 'here are sume numbers: 12²569³08...'; preg_match('#\d+#', $str, $match); echo $match[0]; Output: 12²569³08 Granted, we don't run into exponents very often (or even at all for some / most people). But would be nice to have a shorthand character class that ONLY matches strictly 0-9 and nothing more. Quote Link to comment https://forums.phpfreaks.com/topic/138921-solved-regular-expression-error/#findComment-726726 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.