Help with a simple reg exp

Results 1 to 2 of 2

Thread: Help with a simple reg exp

  1. #1
    Join Date
    Dec 1969

    Default Help with a simple reg exp

    Hello,<BR><BR>This is so stupid but I can&#039;t figure it out. I need to test a string and make sure the following:<BR><BR>That it is not more than 16 characters long<BR>It only contains the following chars: a-zA-Z0-9<BR><BR>I&#039;ve tried the following:<BR>[a-zA-Z0-9]<BR>^[a-zA-Z0-9]$<BR>(i wasn&#039;t even worried about length here yet)<BR>etc...<BR><BR>I&#039;m somewhat new to regular expressions, but I know this should be pretty simple.<BR><BR>Any help would be greatly appreciated.<BR>-Greg

  2. #2
    Join Date
    Dec 1969

    Default RE: Help with a simple reg exp

    You were on the right track when you had:<BR><BR> ^[a-zA-Z0-9]$<BR><BR>The only problem was that you only allow 1 character between the ^ and $. To allow for multiple characters you could have applied the + metacharacter like so:<BR><BR> ^[a-zA-Z0-9]+$<BR><BR>To limit the number of matches you can use the {x,y} syntax - where x is the minimum number of matches and y is the upper limit. In your case, something like this should do the trick:<BR><BR> ^[a-zA-Z0-9]{1,16}$<BR><BR>It&#039;s worth mentioning that, you can omit the upper limit like so:<BR><BR> ^[a-zA-Z0-9]{1,}$<BR><BR><BR>which is - in effect - the same as writing the original pattern with the + metacharacter:<BR><BR> ^[a-zA-Z0-9]+$

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts