Jump to content

[SOLVED] Little help with a regular expression


Wolphie

Recommended Posts

Hi,

  I'm totally new to regular expressions. So I'm looking for a bit of guidance and descriptions. My problem is that I'm trying to validate a name and description of a category for example, the name and description should only contain upper and lower case letters from A-Z and numbers from 0-9 with no special characters, not even an underscore. I also want to be able to limit the length of the string using regular expressions.

 

Here is what I have

if (!preg_match('[A-Za-z][A-Za-z0-9]{0,70}+', $str) {
// Error...
}

 

Could anybody provide any guidance and/or examples and a description of the operators used in regular expressions? e.g., + / *

 

Much appreciated, thanks.

 

Link to comment
Share on other sites

This will do the job. When you put "^" inside the box first like so, it's identical to a "NOT" operator. So if it finds a match that is NOT A-Za-z0-9, it will be true. Otherwise, no matches and it's false.

<?php
if(preg_match('/[^A-Za-z0-9]*/',$str)){
// Error
}
?>

 

The below code will produce identical results as well:

 

<?php
if(!preg_match('/[A-Za-z0-9]*/',$str)){
// Error
}
?>

 

The only difference is instead of using "^", I used "!" with the if/else statement.

 

Also note that the asterisk means to match zero or more times.

Link to comment
Share on other sites

Here is a description of your original regex:

Part 1: []

Match a single character in the range between A and Z

Match a single character in the range between a and z

Part 2: []

Match a character in the list below between 0 and 70 times, as many times as possible, without giving back (possessive)

Match a single character in the range between A and Z

Match a single character in the range between a and z

Match a single character in the range between 0 and 9

 

Link to comment
Share on other sites

Thanks for the description both of you. It's much appreciated.

 

kratsg, do you have any advice about limiting the length of the string? Between 0 and 70 characters long?

 

Also, if possible could you explain what the "+", "/" and rules enclosed in braces and parenthesis is used for please?

 

Thanks.

Link to comment
Share on other sites

Here is a description of your original regex:

Part 1: []

Match a single character in the range between A and Z

Match a single character in the range between a and z

Part 2: []

Match a character in the list below between 0 and 70 times, as many times as possible, without giving back (possessive)

Match a single character in the range between A and Z

Match a single character in the range between a and z

Match a single character in the range between 0 and 9

 

 

Actually, that is false.

 

Match a character in the list below between 0 and 70 times, as many times as possible, without giving back (possessive)

 

(0,70) won't do that... however, {0,70} will. Anything inside parentheses is like brackets, except that you can use parentheses for backreferences in order to use preg_replace, etc...

 

Here's a quickie explanation:

 

\  the escape character - used to find an instance of a metacharacter like a period, brackets, etc.

. (period) match any character except newline

x match any instance of x

^x match any character except x

[x] match any instance of x in the bracketed range - [abxyz] will match any instance of a, b, x, y, or z

| (pipe) an OR operator - [x|y] will match an instance of x or y

() used to group sequences of characters or matches

{} used to define numeric quantifiers

{x} match must occur exactly x times

{x,} match must occur at least x times

{x,y} match must occur at least x times, but no more than y times

? preceding match is optional or one only, same as {0,1}

* find 0 or more of preceding match, same as {0,}

+ find 1 or more of preceding match, same as {1,}

^ match the beginning of the line

$ match the end of a line

 

 

Character Classes

\d  matches a digit, same as [0-9]

\D matches a non-digit, same as [^0-9]

\s matches a whitespace character (space, tab, newline, etc.)

\S matches a non-whitespace character

\w matches a word character

\W matches a non-word character

\b matches a word-boundary (NOTE: within a class, matches a backspace)

\B matches a non-wordboundary

Link to comment
Share on other sites

Okay, I'm beginning to understand it a bit more now.

 

So, I'll try again based on my original rule..

if (preg_match('/[^A-Za-z\d]{0,70}/', $str) {
  // Error...
}

Would that do as I explained before? Or is "{0,70" still incorrect? If so, could you provide an example please?

Link to comment
Share on other sites

That confused me:

 

Do you want to check to see if it is between 0 and 70 characters in length or do you only want to check 0-70 characters of that string?

 

To check length, use the strlen($str) function:

 

<?php

if(!strlen($str) <= 70){//error, it is not between 0 and 70 characters
} else {//no error, it is between 0 and 70 characters
}
?>

Link to comment
Share on other sites

Oh I know about strlen, but I just heard that you could check the length of the string using regular expressions. I guess you can only check how many iterations of the rule has been performed. Thanks, guys, this solved my problem and thanks for the detailed description kratsg. Thanks again neil.

Link to comment
Share on other sites

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.