PHP Regular Expression. Check if String contains ONLY letters - php

In PHP, how do I check if a String contains only letters? I want to write an if statement that will return false if there is (white space, number, symbol) or anything else other than a-z and A-Z.
My string must contain ONLY letters.
I thought I could do it this way, but I'm doing it wrong:
if( ereg("[a-zA-Z]+", $myString))
return true;
else
return false;
How do I find out if myString contains only letters?

Yeah this works fine. Thanks
if(myString.matches("^[a-zA-Z]+$"))

Never heard of ereg, but I'd guess that it will match on substrings.
In that case, you want to include anchors on either end of your regexp so as to force a match on the whole string:
"^[a-zA-Z]+$"
Also, you could simplify your function to read
return ereg("^[a-zA-Z]+$", $myString);
because the if to return true or false from what's already a boolean is redundant.
Alternatively, you could match on any character that's not a letter, and return the complement of the result:
return !ereg("[^a-zA-Z]", $myString);
Note the ^ at the beginning of the character set, which inverts it. Also note that you no longer need the + after it, as a single "bad" character will cause a match.
Finally... this advice is for Java because you have a Java tag on your question. But the $ in $myString makes it look like you're dealing with, maybe Perl or PHP? Some clarification might help.

Your code looks like PHP. It would return true if the string has a letter in it. To make sure the string has only letters you need to use the start and end anchors:
In Java you can make use of the matches method of the String class:
boolean hasOnlyLetters(String str) {
return str.matches("^[a-zA-Z]+$");
}
In PHP the function ereg is deprecated now. You need to use the preg_match as replacement. The PHP equivalent of the above function is:
function hasOnlyLetters($str) {
return preg_match('/^[a-z]+$/i',$str);
}

I'm going to be different and use Character.isLetter definition of what is a letter.
if (myString.matches("\\p{javaLetter}*"))
Note that this matches more than just [A-Za-z]*.
A character is considered to be a letter if its general category type, provided by Character.getType(ch), is any of the following: UPPERCASE_LETTER, LOWERCASE_LETTER, TITLECASE_LETTER, MODIFIER_LETTER, OTHER_LETTER
Not all letters have case. Many characters are letters but are neither uppercase nor lowercase nor titlecase.
The \p{javaXXX} character classes is defined in Pattern API.

Alternatively, try checking if it contains anything other than letters: [^A-Za-z]

The easiest way to do a "is ALL characters of a given type" is to check if ANY character is NOT of the type.
So if \W denotes a non-character, then just check for one of those.

Related

Match multiple characters without repetion on a regular expression

I'm using PHP's PCRE, and there is one bit of the regex I can't seem to do. I have a character class with 5 characters [adjxz] which can appear or not, in any order, after a token (|) on the string. They all can appear, but they can only each appear once. So for example:
*|ad - is valid
*|dxa - is valid
*|da - is valid
*|a - is valid
*|aaj - is *not* valid
*|adjxz - is valid
*|addjxz - is *not* valid
Any idea how I can do it? a simple [adjxz]+, or even [adjxz]{1,5} do not work as they allow repetition. Since the order does not matter also, I can't do /a?d?j?x?z?/, so I'm at a loss.
Perhaps using a lookahead combined with a backreference like this:
\|(?![adjxz]*([adjxz])[adjxz]*\1)[adjxz]{1,5}
demonstration
If you know these characters are followed by something else, e.g. whitespace you can simplify this to:
\|(?!\S*(\S)\S*\1)[adjxz]{1,5}
I think you should break this in 2 steps:
A regex to check for unexpected characters
A simple PHP check for duplicated characters
function strIsValid($str) {
if (!preg_match('/^\*|([adjxz]+)$/', $str, $matches)) {
return false;
}
return strlen($matches[1]) === count(array_unique(str_split($matches[1])));
}
I suggest using reverse logic where you match the unwanted case using this pattern
\|.*?([adjxz])(?=.*\1)
Demo

PHP, Regex - how to disallow non-alphanumeric characters

I'm trying to make a regex that would allow input including at least one digit and at least one letter (no matter if upper or lower case) AND NOTHING ELSE. Here's what I've come up with:
<?php
if (preg_match('/(?=.*[a-z]+)(?=.*[0-9]+)([^\W])/i',$code)) {
echo "=)";
} else {
echo "=(";
}
?>
While it gives false if I use only digits or only letters, it gives true if I add $ or # or any other non-alphanumeric sign. Now, I tried putting ^\W into class brackets with both a-z and 0-9, tried to use something like ?=.*[^\W] or ?>! but I just can't get it work. Typing in non-alphanums still results in true. Halp meeee
You need to use anchors so that it matches against the entire string.
^(?=.*[a-z]+)(?=.*[0-9]+)(\w+)$
Since you are using php, why even use regex at all. You can use ctype_alnum()
http://php.net/manual/en/function.ctype-alnum.php

Function that returns true if string is a pronoun

I am trying to build a function that returns true if a given string, as an argument, is a pronoun.
I figured a good way to do this is to check if it begins with a capital / uppercase letter.
If you know of a better what to tell if something is a pronoun, please let me know.
But, for the checking if the first letter is a capital / upper case letter, how can I do that.
I know string[0] would give me the first letter, but what do I compare it to to check if it is any capital / upper case letter in the alphabet?
The code that I have right now is
function isPronoun($str){
$result = false;
if($str[0]===/*capital letter*/){
$result=true;
}
return $result;
}
Proper nouns such as "Mark", "London" or "Betelgeuse" also begin with a capital letter, as does the first word of a sentence, while "he" or "it" are pronouns that begin with a lowercase letter.
To identify a pronoun, you need a language parser such as a Brill Parser.
try with regular expression
function isPronoun($str){
return ereg("^[A-Z]", $str);
}
I think you mean "Proper Noun", not pronoun. Pronouns are things like "I", "you"...
Anyway, try something like this:
$is_proper_noun = preg_match("(^[A-Z]\w+$)",$word);
Note that you can make this more elaborate, for instance:
"(^(?:Ma?c|O')?[A-Z][a-z]+$)"

How can I find Alphanumeric in a string

$foo = "username122";
pre_match('Contain only aplhanumeric string', $foo){
return true;
}
$foo Contain Only alphanumeric not special characters (=\*-[( etc)
ctype_alnum() function will do you dandy :)
Use a regular expression matching on alphanumeric characters only from beginning to end:
/^[A-Za-z0-9]*$/
For example:
$testRegex = "/^[A-Za-z0-9]*$/";
$testString = "abc123";
if (preg_match($testRegex, $testString)) {
// yes, the string is entirely alphanumeric
} else {
// no, the string is not entirely alphanumeric
}
if(preg_match('~[^a-z0-9 ]~i', $foo)) {
//DO SOMETHING HERE;
}
Your regular expression for something like this won't change between PHP and JavaScript. In JavaScript it's an object, whereas in PHP it's a string, but the pattern is still the same:
/^[a-z0-9]+$/i
Where ^ represents the start of the string and $ represents the end of the string. Next, a-z matches any letter, and 0-9 matches any number. The + states that the previous pattern could be repeated one or more times. The i modifier makes the a-z portion case-insensitive, so upper and lower case are matched.
Testing in JavaScript:
/^[a-z0-9]+$/i.test("Foo123"); // true
Testing in PHP:
preg_match("/^[a-z0-9]+$/i", "Foo123"); // 1
With PHP, you have the option of using POSIX character classes, such as :alnum:. Please note that this won't work in JavaScript:
preg_match("/^[[:alnum:]]+$/i", "Foo123"); // 1
There's actually a much easier method for testing in PHP, using the Ctype functions, specifically the ctype_alnum function, which will return a boolean value stating whether all characters in a given string are alphanumeric or not:
ctype_alnum("Foo123"); // true
ctype_alnum("Foo!23"); // false

Regular expression to check characters in a string

I have a text to validate using regex,
Text field is allowed to have all the characters a-z and 0-9 except for alphabets (i,o,q).
I tried something like this but cannot get it to work '/[^(oOiIqQ)]/'
A simple way for exclusions like this is to use negative lookahead. State what you want:
/^(?:[a-z0-9])+\z/i
Then exclude the items you don't want:
/^(?:(?![ioq])[a-z0-9])+\z/i
You cannot use parenthesis in [ ... ].
You have to use something like '/[0-9a-hj-npr-zA-HJ-NPR-Z]/'
If you want to be sure your text only has those characters, use:
'/^[0-9a-hj-npr-zA-HJ-NPR-Z]+$/'
So you can match a string containing any number of those characters, and only those.
Maybe someting like this /^[0-9a-hj-npr-z A-HJ-NPR-X]+$/
I would assume a little change to your's and would try:
^[^oOiIqQ]+$
This might works: [a-hj-npr-z] Maybe you can add the flag i at the end of your regexp for case insensibility.
(yours will allow EVERY characters except those you specified)
if (preg_match('#^[0-9a-hj-npr-z]+$#i', $string)) {
//string OK
}
There is a very simple solution for this which takes in consideration negative regexing (which makes the regex shorter and much readable)
[^ioqIOQ]+

Categories