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
Related
I have the following requirements for validating an input field:
It should only contain alphabets and spaces between the alphabets.
It cannot contain spaces at the beginning or end of the string.
It cannot contain any other special character.
I am using following regex for this:
^(?!\s*$)[-a-zA-Z ]*$
But this is allowing spaces at the beginning. Any help is appreciated.
For me the only logical way to do this is:
^\p{L}+(?: \p{L}+)*$
At the start of the string there must be at least one letter. (I replaced your [a-zA-Z] by the Unicode code property for letters \p{L}). Then there can be a space followed by at least one letter, this part can be repeated.
\p{L}: any kind of letter from any language. See regular-expressions.info
The problem in your expression ^(?!\s*$) is, that lookahead will fail, if there is only whitespace till the end of the string. If you want to disallow leading whitespace, just remove the end of string anchor inside the lookahead ==> ^(?!\s)[-a-zA-Z ]*$. But this still allows the string to end with whitespace. To avoid this look back at the end of the string ^(?!\s)[-a-zA-Z ]*(?<!\s)$. But I think for this task a look around is not needed.
This should work if you use it with String.matches method. I assume you want English alphabet.
"[a-zA-Z]+(\\s+[a-zA-Z]+)*"
Note that \s will allow all kinds of whitespace characters. In Java, it would be equivalent to
[ \t\n\x0B\f\r]
Which includes horizontal tab (09), line feed (10), carriage return (13), form feed (12), backspace (08), space (32).
If you want to specifically allow only space (32):
"[a-zA-Z]+( +[a-zA-Z]+)*"
You can further optimize the regex above by making the capturing group ( +[a-zA-Z]+) non-capturing (with String.matches you are not going to be able to get the words individually anyway). It is also possible to change the quantifiers to make them possessive, since there is no point in backtracking here.
"[a-zA-Z]++(?: ++[a-zA-Z]++)*+"
Try this:
^(((?<!^)\s(?!$)|[-a-zA-Z])*)$
This expression uses negative lookahead and negative lookbehind to disallow spaces at the beginning or at the end of the string, and requiring the match of the entire string.
I think the problem is there's a ? before the negation of white spaces, which means it is optional
This should work:
[a-zA-Z]{1}([a-zA-Z\s]*[a-zA-Z]{1})?
at least one sequence of letters, then optional string with spaces but always ends with letters
I don't know if words in your accepted string can be seperated by more then one space. If they can:
^[a-zA-Z]+(( )+[a-zA-z]+)*$
If can't:
^[a-zA-Z]+( [a-zA-z]+)*$
String must start with letter (or few letters), not space.
String can contain few words, but every word beside first must have space before it.
Hope I helped.
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.)
I'm trying to match all occurances of "string" in something like the following sequence except those inside ##
as87dio u8u u7o #string# ou os8 string os u
i.e. the second occurrence should be matched but not the first
Can anyone give me a solution?
You can use negative lookahead and lookbehind:
(?<!#)string(?!#)
EDIT
NOTE: As per Marks comments below, this would not match #string or string#.
You can try:
(?:[^#])string(?:[^#])
OK,
If you want to NOT match a character you put it in a character class (square brackets) and start it with the ^ character which negates it, for example [^a] means any character but a lowercase 'a'.
So if you want NOT at-sign, followed by string, followed by another NOT at-sign, you want
[^#]string[^#]
Now, the problem is that the character classes will each match a character, so in your example we'd get " string " which includes the leading and trailing whitespace. So, there's another construct that tells you not to match anything, and that is parens with a ?: in the beginning. (?: ). So you surround the ends with that.
(?:[^#])string(?:[^#])
OK, but now it doesn't match at the start of string (which, confusingly, is the ^ character doing double-duty outside a character class) or at the end of string $. So we have to use the OR character | to say "give me a non-at-sign OR start of string" and at the end "give me an non-at-sign OR end of string" like this:
(?:[^#]|^)string(?:[^#]|$)
EDIT: The negative backward and forward lookahead is a simpler (and clever) solution, but not available to all regular expression engines.
Now a follow-up question. If you had the word "astringent" would you still want to match the "string" inside? In other words, does "string" have to be a word by itself? (Despite my initial reaction, this can get pretty complicated :) )
How do I make the following regular expression accept only the symbols I want it to accept as well as spaces?
if(!preg_match('/^[A-Z0-9\/\'&,.-]*$/', $line))
{
die();
}
else
{
//execute the rest of the validation script
}
I want the user to only be able to enter A-Z, 0-9, forward slashes, apostrophes, ampersands, commas, periods, and hyphens into a given text field $line.
It currently will accept something along the lines of HAM-BURGER which is perfect, it should accept that. I run into an issue when the user wants to type HAM BURGER (<- note the space).
If I remove the ^ from the beginning and/or the $ from the end it will succeed if the user types in anything. My attempted remedy to this was to make the * into a + but then it will accept anything as long as the user puts in at least one of the acceptable characters.
Add the space to the character class:
if(!preg_match('/^[A-Z0-9\/\'&,. -]*$/', $line))
Yes, it's that simple.
Note that the space has to be inserted before the - because it is a metacharacter in a character class (unless it's the first or last character in said character class). Another option is to escape it like:
if(!preg_match('/^[A-Z0-9\/\'&,.\- ]*$/', $line))
The regex explained:
^ and $ are start and end of string anchors. It tells the regex engine that it has to match the whole string rather than just part of it.
[...] is a character class.
* is the zero-or-more repetition operator. This means it will accept an empty string. You can change it to + (one-or-more) so it rejects the empty string.
This is a good reference for RegEx, though specifically for Perl:
http://www.cs.tut.fi/~jkorpela/perl/regexp.html
I want to accept a list of character as input from the user and reject the rest. I can accept a formatted string or find if a character/string is missing.
But how I can accept only a set of character while reject all other characters. I would like to use preg_match to do this.
e.g. Allowable characters are: a..z, A..Z, -, ’ ‘
User must able to enter those character in any order. But they must not allowed to use other than those characters.
Use a negated character class: [^A-Za-z-\w]
This will only match if the user enters something OTHER than what is in that character class.
if (preg_match('/[^A-Za-z-\w]/', $input)) { /* invalid charcter entered */ }
[a-zA-Z-\w]
[] brackets are used to group characters and behave like a single character. so you can also do stuff like [...]+ and so on
also a-z, A-Z, 0-9 define ranges so you don't have to write the whole alphabet
You can use the following regular expression: ^[a-zA-Z -]+$.
The ^ matches the beginning of the string, which prevents it from matching the middle of the string 123abc. The $ similarly matches the end of the string, preventing it from matching the middle of abc123.
The brackets match every character inside of them; a-z means every character between a and z. To match the - character itself, put it at the end. ([19-] matches a 1, a 9, or a -; [1-9] matches every character between 1 and 9, and does not match -).
The + tells it to match one or more of the thing before it. You can replace the + with a *, which means 0 or more, if you also want to match an empty string.
For more information, see here.
You would be looking at a negated ^ character class [] that stipulates your allowed characters, then test for matches.
$pattern = '/[^A-Za-z\- ]/';
if (preg_match($pattern, $string_of_input)){
//return a fail
}
//Matt beat me too it...