Regular expression matching more than allowed characters - php

I am trying to validate that the given string contains contains only letters, numbers, spaces, and characters from a set of symbols (!-?():&,;+). Here is what I have so far:
/^[a-zA-Z0-9 !-?\(\):&,;\+]+$/
Now this works somewhat but it accepts other characters as well. For example, strings containing * or # validate. I thought that the ^ at the beginning of the expression and the $ at the end meant that it would match the whole string. What am I doing wrong?
Thanks.

/^[a-zA-Z0-9 !-?\(\):&,;\+]+$/
The - is not nice where you placed it! If you want to place - inside a character class be sure to either place it first or last e.g.
/^[a-zA-Z0-9 !?\(\):&,;\+-]+$/
Otherwise it will take the range of ! until ? whatever this range maybe...Depends on your regex machine.
Finally special characters are not special inside character classes. So no need to escape most of them :
/^[a-zA-Z0-9 !?():&,;+-]+$/

You have specified a "range" within your character class:
[!-?]
Means all ASCII symbols between ! and ?
http://www.regular-expressions.info/charclass.html
You need to escape the minus - with a \ backslash. (OTOH the backslash is redundant before the + and ( and ) within a character class.)

Related

Preg_match php explaination

I have a question regarding one character in the preg_match syntax below.
I just want to completely understand.
\w looking for alpha-numberic characters and the underscore.
My question is what does the \ mean after \w and before the # sign?
Does this mean that it will allow:
any alphanumeric
any backslash
any dash
or is this backslash meant to single out the character that follows?
When I test it in w3schools.com example I can have backslashes in the email address which validates but they are removed when they are echoed out.
$email = test_input($_POST["email"]);
// check if e-mail address syntax is valid
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/",$email))
{
$emailErr = "Invalid email format";
}
The backslash is used to escape characters that have a special meaning in a regex to obtain a literal character. There are twelve characters that must be escaped: [ { ( ) . ? * + | \ ^ $
If I want to write a literal $ in a pattern, I must write \$
Note: you don't need to escape { if the situation is no ambiguous (with the quantifier {m,n} or {m})
Note 2: The delimiter of the pattern must be escaped too, inside and outside a character class.
Inside a character class these twelve characters don't need no more to be escaped since they loose their special meaning and are seen as literals. However, there is three characters that have a special meaning if they are in a special position in the character class. These characters are: ^ - ]
^ at the first position is used to negate a character class ([^M] => all that is not a M ). If you want to use it as a literal character at "the first position", you must write: [\^]
- between two characters defines a character range ([a-z]). This means that you don't need to escape it at the begining (or immediatly after ^) or at the end of the class. You only need to escape it between two characters. - is seen as a literal (and doesn't define a range) in all these examples:
[-abcd]
[^-abcd]
[abcd-]
[ab\-cd]
[\s-abcd] # because \s is not a character
] since it is used to close the character class must be escaped except at the first position or immediatly after the ^. []] and [^]] are correct.
If I write the pattern without uneeded backslashes, I obtain:
/([\w-]+#[\w-]+\.[\w-]+)/
To answer your question ("What does it mean?"): Nothing, uneeded escapes are ignored by the regex engine.

allow parentheses and other symbols in regex

I've made this regex:
^[a-zA-Z0-9_.-]*$
Supports:
letters [uppercase and lowercase]
numbers [from 0 to 9]
underscores [_]
dots [.]
hyphens [-]
Now, I want to add these:
spaces [ ]
comma [,]
exclamation mark [!]
parenthesis [()]
plus [+]
equal [=]
apostrophe [']
double quotation mark ["]
at [#]
dollar [$]
percent [%]
asterisk [*]
For example, this code accept only some of the symbols above:
^[a-zA-Z0-9 _.,-!()+=“”„#"$#%*]*$
Returns:
Warning: preg_match(): Compilation failed: range out of order in character class at offset 16
Make sure to put hyphen - either at start or at end in character class otherwise it needs to be escaped. Try this regex:
^[a-zA-Z0-9 _.,!()+=`,"#$#%*-]*$
Also note that because * it will even match an empty string. If you don't want to match empty strings then use +:
^[a-zA-Z0-9 _.,!()+=`,"#$#%*-]+$
Or better:
^[\w .,!()+=`,"#$#%*-]+$
TEST:
$text = "_.,!()+=,#$#%*-";
if(!preg_match('/\A[\w .,!()+=`,"#$#%*-]+\z/', $text)) {
echo "error.";
}
else {
echo "OK.";
}
Prints:
OK.
The hyphen is being treated as a range marker -- when it sees ,-! it thinks you're asking for a range all characters in the charset that fall between , and ! (ie the same way that A-Z works. This isn't what you want.
Either make sure the hyphen is the last character in the character class, as it was before, or escape it with a backslash.
I would also point out that the quote characters you're using “”„ are part of an extended charset, and are not the same as the basic ASCII quotes "'. You may want to include both sets in your pattern. If you do need to include the non-ASCII characters in the pattern, you should also add the u modifier after the end of your pattern so it correctly picks up unicode characters.
Try escaping your regex: [a-zA-Z0-9\-\(\)\*]
Check if this help you: How to escape regular expression special characters using javascript?
Inside of a character class [...] the hyphen - has a special meaning unless it is the first or last character, so you need to escape it:
^[a-zA-Z0-9 _.,\-!()+=“”„#"$#%*]*$
None of the other characters need to be escaped in the character class (except ]). You will also need to escape the quote indicating the string. e.g.
'/[\']/'
"/[\"]/"
try this
^[A-Z0-9][A-Z0-9*&!_^%$#!~#,=+,./\|}{)(~`?][;:\'""-]{0,8}$
use this link to test
trick is i reverse ordered the parenthesis and other braces that took care of some problems. And for square braces you must escape them

How do you allow '-' in regular expression?

I am trying to allow '-' in the regular expression for telephone numbers, but the - is usually used for ranges (e.g. A-Z). So how do I allow just the character? I tried escapting using /-, but that's not working.
$reg_num = "/[^0-9+ ()]/";
You need to escape it with a backslash \. So it should be written as \-.
Write it in the end instead of '-' being between two characters.
Very simplified example:
[0-9-] would match 099-2233-3333 where 0-9 is a range, and the - in the end is a seperate dash sign to match.
Put it first in the range, like [^-0-9+ ()]. The hyphen needs to separate two characters to define a range; if it isn't (in this case because the ^ is also interpreted as a modifier, not a character in the set), then it's just a character in the set like any other.
escaping using \-
\
is escape character!

Help with php regex for limiting allowed characters

I'm working in php and want to set some rules for a submitted text field. I want to allow letters, numbers, spaces, and the symbols # ' , -
This is what I have:
/^(a-z,0-9+# )+$/i
That seems to work but when I add the ' or - symbols I get errors.
Almost there. What you're looking for is called character classes. These are denoted by the use of square brackets. For example
/^[-a-z0-9+#,' ]+$/i
To include the hyphen character, it needs to be the first or last character in the class.
Edit
As you want to include the single quote and you're using PHP where regular expressions must be represented as strings, be careful with how you quote the pattern. In this case, you can use either of
$pattern = "/^[-a-z0-9+#,' ]+\$/i"; // or
$pattern = '/^[-a-z0-9+#,\' ]+$/i';
You should use a character class - [a-zA-Z0-9 #',-]
Note that - should be used first or last or escaped otherwise it gets treated as denoting a range and you will get errors
I want to allow letters, numbers, spaces, and the symbols #, ', , and -.
Use this regex...
/^[-a-zA-Z\d ',#]+\z/
Note the \z. If you use $, you are allowing a trailing \n. CodePad.
Ensure to escape the ' if you are using ' as your string delimiter.
Please use /^[a-z,0-9+\#\-,\s]+$/i
Use this regex:
/^[-a-z0-9,# ']+$/i

php regex allow only english characters in string

I want to insert into a textbox only english characters and other special characters like
$!#{]{[
etc...
but also i want to check if the string contains at least 2 characters of these: (a-zA-Z0-9)
So i thought of this:
preg_match('/[^a-zA-Z0-9 -"?()[]#:/\'_+*%#!~`$><,.;{}|\]/',$string)
is this a good approach?
No your approach is not good
Try this one. You need to complete the special characters you want into the character class. You need to escape the ]\-^ characters since they have special meanings in the class (depending on their position).
^(?=.*[A-Za-z0-9].*[A-Za-z0-9])[$!#{}[\]A-Za-z0-9]*$
See it here on Regexr
The first part is a positive lookahead that ensures the two characters of your [A-Za-z0-9] requirement somewhere in the string.
Then comes the character class [A-Za-z0-9])[$!#\{\}\[\]A-Za-z0-9] where you can put in the characters that you want to match.
The ^ at the beginning of my expression ensures that it matches from the start of the beginning and the $ at the end ensure that it matches the end of the string.
The ^ at the beginning of your example is a negation of the complete character class, what you don't want I guess, if you want to match for the character ^ put it somewhere else in the class. The - in the middle of your class defines a character range that matches everything from -", I don't know what characters that are, but probably more than you want. Put the - at the beginning or the end or escape it.
(?:.*?[0-9a-zA-Z]){2,}[0-9a-zA-Z$!#{\]{\[]*
$parameter ='(a-zA-Z){2}';
if $string='kasdfhk890';
preg_match($string,$parameter); //
return false;
if $string='k';
preg_match($string,$parameter); // single char error
return false;
if $string='kuyyee';
preg_match($string,$parameter);// english character only
return true;
You want learn more try this link

Categories