How validate username using regexp ?
For English letters, Numbers and spaces I am using :
/^[a-zA-Z]{1}([a-zA-Z0-9]|\s(?!\s)){4,14}[^\s]$/
How can i add arabic letters ?
Well that would depend if your characters are coming in as cp1256 or unicode. If its unicode you can use the range such as #([\x{0600}-\x{06FF}]+\s*) in your expression.
you would use unicode regexes and match all letters:
/\pL+/u
(one or more letters)
Related
I used ctype_alpha() on a input so someone can't write numbers or special signs.
ctype_alpha($_POST["name"])
But i just noticed a problem today. This only check A-Z and not special letters like Å Ä Ö.
How should i structure this instead? So you can't write numbers and special signs but you can write special letters?
Thanks in advance!
Use a regexp that matches a Unicode character class. For example, \p{L} to match letters:
if (preg_match('/^\p{L}*$/', $_POST['name'])) ...
This is my regular expression code:
"onlyLetterSp": {
"regex": /^[a-zA-Z\ \']+$/,
"alertText": "* Letters only"
}
How can I change this to allow English characters as well as Japanese?
I found this link:
http://www.localizingjapan.com/blog/2012/01/20/regular-expressions-for-japanese-text/
There are apparently a few different character sets for different types of Japanese.
Hiragana for example is:
[\x3041-\x3096]
You must be looking for the u regex modifier, which stands for Unicode. With it you can use POSIX symbols like \w to include whatever "word" characters you like
Testing the PHP regex engine, I see that it considers only [0-9A-Za-z_] to be word characters. Letters of non-ASCII languages, such as Hebrew, are not matched as word characters with [\w]. Are there any PHP or Perl regex escape sequences which will match a letter in any language? I could add ranges for each alphabet that I expect to be used, but users will always surprise us with unexpected languages!
Note that this is not for security filtering but rather for tokenizing a text.
Try [\pL_] - see the reference at
http://php.net/manual/en/regexp.reference.unicode.php
Try \p{L}. It matches any kind of letter from any language. If you don't want to use char set [].
I need a regular expression that accepts only Greek chars and spaces for a name field in my form (PHP).
I've tried several findings on the net but no luck. Any help will be appreciated.
Full letters solution, with accented letters:
/^[A-Za-zΑ-Ωα-ωίϊΐόάέύϋΰήώ]+$/
I'm not too current on the Greek alphabet, but if you wanted to do this with the Roman alphabet, you would do this:
/^[a-zA-Z\s]*$/
So to do this with Greek, you replace a and z with the first and last letters of the Greek alphabet. If I remember right, those are α and ω. So the code would be:
/^[α-ωΑ-Ω\s]*$/
The other answers here didn't work for me. Greek Unicode characters are included in the following two blocks
Greek and Coptic U+0370 to U+03FF (normal Greek letters)
Greek Extended U+1F00 to U+1FFF (Greek letters with diacritics)
The following regex matches whole Greek words:
[\u0370-\u03ff\u1f00-\u1fff]+
I will let the reader translate that to whichever programming language format they may be using.
See also
Unicode charts
To elaborate on leo pal's answer, an even more complete regex, which would accept even capital accented Greek characters, would be the following:
/^[α-ωΑ-ΩίϊΐόάέύϋΰήώΊΪΌΆΈΎΫΉΏ\s]+$/
With this, you get:
α-ω - lowercase letters
Α-Ω - uppercase letters
ίϊΐόάέύϋΰήώ - lowercase letters with all (modern) diacritics
ΊΪΌΆΈΎΫΉΏ - uppercase letters with all (modern) diacritics
\s - any whitespace character
Note: The above does not take into account ancient Greek diacritics (ᾶ, ἀ, etc.).
What worked for me was /^[a-zA-Z\p{Greek}]+$/u
source: http://php.net/manual/fr/function.preg-match.php#105324
Greek & Coptic in utf-8 seem to be in the U+0370 - U+03FF range. Be aware: a space, a -, a . etc. are not....
Just noticed at the excellent site https://regexr.com/ that the range of Greek characters are from "Ά" (902) to "ώ" (974) with 3 characters that are not aphabet characters: "·" (903) and unprintable characters 0907, 0909
So a range [Ά-ώ] will cover 99.99% of the cases!
With (?![·\u0907\u0909])[Ά-ώ] covers 100%. (I don't check this at PHP though)
The modern Greek alphabet in UTF-8 is in the U+0386 - U+03CE range.
So the regex you need to accept Greek only characters is:
$regex_gr = '/^[\x{0386}-\x{03CE}]+$/u';
or (with spaces)
$regex_gr_with_spaces = '/^[\x{0386}-\x{03CE}\s]+$/u';
I'm using the following code to check for a string where all the characters are upper-case letters:
if (preg_match('/^[\p{Lu}]+$/', $word)) {
This works great for English, but fails to detect letters with accents, Russian letters, etc. Is \p{Lu} supposed to work for all languages? Is there a better approach?
A special option is the /u which turns on the Unicode matching mode, instead of the default 8-bit matching mode. You should specify /u for regular expressions that use \x{FFFF}, \X or \p{L} to match Unicode characters, graphemes, properties or scripts. PHP will interpret '/regex/u' as a UTF-8 string rather than as an ASCII string.
http://www.regular-expressions.info/php.html --
using function u can do change in Uppercase of String ....
Function Available here :
string name="manish niitian";
console.Writeline("Your String in Uppercase is : "+name.UPPERCASE());