Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/138921-solved-regular-expression-error/
Share on other sites

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..

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.

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?

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.