Reputation: 418
I'm having trouble with regex. I want to use php preg_match to confirm that a string ONLY contains upper/lower case letters, numbers, spaces, and punctuation like...
comma, period, plus sign, dash, exclamation mark, colon, semi colon, parentheses
The question: what would $regex equal in the example below?
<?php
$regex = "??????";
$string = "some user input in a form passed via POST to form processor";
if (preg_match($regex, $string)) {
echo "Found a match!";
} else {
echo "The regex pattern does not match. :(";
}
?>
I've watched videos, Googled for hours and still can not get this right.
Thanks for any help !
Upvotes: 1
Views: 2062
Reputation: 418
All answers were helpful. The forth bird's answer worked for me. Thanks.
I added a few extra punctuation marks and 3 that needed escaping...and (so far) ended up with this...
$regex = "/^[ a-z0-9,.+!:;()-_\\\[\]]+$/i";
I'm posting an explanation just to help any others that may struggle with this. Hopefully it will help someone as you folks helped me.
Explanation follows for anyone that may need it (corrections welcome if any :-)
string validation: items within in [] are allowed
/ beginning regex expression delimiter
^ says start trying to match with 1st item in []
[ enclouses OK items - start
blank space character
a-z lower case letters
0-9 numbers
, comma
. period
+ plus sign
! exclamation mark
: colon
; semicolon
( open paren
) close paren
- dash
_ underscore
\\ back slash (note)
\[ open square bracket (note)
\] close speare bracket (note)
] encloses OK items - end
+ match indicated items 1 or more times
$ continue matching through the last item in the string
/ is the closing regex expression delimiter
i case insensitive flag so upper and lower case letters will match
note: leading backslash is escape character
Upvotes: 0
Reputation: 2040
/^[a-zA-Z0-9,.!?\-\+:;() ]*$/
See examples: Regex for only allowing letters, numbers, space, commas, periods?
Google search on: preg_match numbers letters punctuation
Edit: you can test here https://www.functions-online.com/preg_match.html
Upvotes: 0
Reputation: 4536
Try this pattern
$regex = "/^[A-Za-z0-9...]+$/";
Where you should replace the 3 dots ...
between 9
and ]
with your allowed special characters.
Example:
// Adding comma (,) only
$regex = "/^[A-Za-z0-9,]+$/";
// Adding comma (,) and period (.)
$regex = "/^[A-Za-z0-9,.]+$/";
// comma and plus sign (+) note that plus/minus signs need to be escaped \+\-
$regex = "/^[A-Za-z0-9,\+\]+$/";
Full string as you asked would be:
// comma, period, plus sign, dash, exclamation mark, colon, semi colon, parentheses, space is (\s)
$regex = "/^[A-Za-z0-9,.\+\-!:;()\s]+$/";
If the Underscore character (_)
is allowed, you could use [\w...]
instead of [A-Za-z0-9...]
You can test it here
Upvotes: 1
Reputation: 163237
You could use a character class to list the characters you want to allow.
If you want to match upper and lowercase characters you can shorten A-Za-z
to a-z
and specify the case insensitive flag /i
.
Assert the position at the start of the string ^
, match the character in your character class one or more times +
and assert the position at the end of the string $
.
<?php
$regex = "/^[ a-z0-9,.+!:;()-]+$/i";
$string = "some user input in a form passed via POST to form processor";
if (preg_match($regex, $string)) {
echo "Found a match!";
} else {
echo "The regex pattern does not match. :(";
}
?>
Upvotes: 1