Regular Expression to match only letters - asp.net

I need write a regular expression for RegularExpressionValidator ASP.NET Web Controls.
The regular expression should ALLOW all alphabetic characters but not numbers or special characters (example: |!"£$%&/().
Any idea how to do it?

^[A-Za-z]+$
validates a string of length 1 or greater, consisting only of ASCII letters.
^[^\W\d_]+$
does the same for international letters, too.
Explanation:
[^ # match any character that is NOT a
\W # non-alphanumeric character (letters, digits, underscore)
\d # digit
_ # or underscore
] # end of character class
Effectively, you get \w minus (\d and _).
Or, you could use the fact that ASP.NET supports Unicode properties:
^\p{L}+$
validates a string of Unicode letters of length 1 or more.

Including spaces:
"^[a-zA-Z ]*$"
Excluding Spaces:
"^[a-zA-Z]*$"
To make it non-optional, change the * to a +

You can use the regex:
^[a-zA-Z]+$
Explanation:
^ : Start anchor
[..] : Char class
+ : one or more repetations
$ : End anchor

Related

Regular expression in c# for input like (integer-anything)

string strRegexclass = #"^([0-9]+)\-([a-zA-Z])$";
I want to make regular expression which accept input like this (1-class). Any integer value before dash(-) and then must have dash then anything after dash.
You can use the code like this:
string strRegexclass = #"^\d+-.*$";
Or you can use the next code
string strRegexclass = #"^\d+-\w*$";
if you want to allow only letters after the dash.
If you plan to only match a string that starts with 1 or more ASCII digits, then a hyphen, and then any 0+ chars use:
^[0-9]+-.*$
See the regex demo
Note that \d and [0-9] are not equal in .NET regex flavor.

Regular expression for one Alphabet and many numbers

I need a regular expression that check my string contains one alphabet+many digits.Eg, A546456 or B456 or N455,asp.net
I have tried
^[a-z|A-Z|]+[a-z|A-Z|0-9]+[0-9]*
and
[a-zA-Z0-9]+
A regular expression to validate if a string starts with a single letter A-Z in any case and has 1 or more digits and nothing else is: ^[A-Za-z]\d+$
Explanation:
^ ... beginning of string (or beginning of line in other context).
[A-Za-z] ... a character class definition for a single character (no multiplier appended) matching a letter in range A-Z in any case. \w cannot be used as it matches also an underscore and letters from other languages as for example German umlauts.
\d ... a digit which is equal the expression [0-9].
+ ... previous expression (any digit) 1 or more times.
$ ... end of string (or end of line in other context).
Be careful with a regular expression containing a backslash as used here for \d put into a string as the backslash character is in many programming/scripting languages also the escape character inside strings and must be therefore often escaped with one more backslash, i.e. "^[A-Za-z]\\d+$"

Regular Expression to get A-Z, excluding other characters and numbers

I have a field Facility with some of the following records:
ABC-XY
ABC-ZZ
EFG-AA
NM
NM-100
NM-202
HYK-109
LI-022
How can I get the letters before the hyphen, but also get the letters when no hyphen is present (like in NM) ?
Your regex should be:
^ [A-Z]+
^ is the start of the string, allowed character class A-Z can present more than once until some other character is found.

regex to allow space if followed by character

I have an asp.net regularexpressionvalidator that I need to match on a textbox. If there is any text, logically the rules are as follows:
The text must be at least three characters, after any trimming to remove spaces.
Characters allowed are a-zA-Z0-9-' /\&.
I'm having major pain trying to construct an expression that will allow a space as the thrid character only if there is a fourth non-space character.
Can anyone suggest an expression? My last attempt was:
^[a-zA-Z0-9-'/\\&\.](([a-zA-Z0-9-'/\\&\.][a-zA-Z0-9-' /\\&\.])|([a-zA-Z0-9-' /\\&\.][a-zA-Z0-9-'/\\&\.]))[a-zA-Z0-9-' /\\&\.]{0,}$
but that does not match on 'a a'.
Thanks.
OK, now this is all in one regex:
^\s*(?=[a-zA-Z0-9'/\\&.-])([a-zA-Z0-9'/\\&.\s-]{3,})(?<=\S)\s*$
Explanation:
^ # Start of string
\s* # Optional leading whitespace, don't capture that.
(?= # Assert that...
[a-zA-Z0-9'/\\&.-] # the next character is allowed and non-space
)
( # Match and capture...
[a-zA-Z0-9'/\\&.\s-]{3,} # three or more allowed characters, including space
)
(?<=\S) # Assert that the previous character is not a space
\s* # Optional trailing whitespace, don't capture that.
$ # End of string
This matches
abc
aZ- &//
a ab abc x
aaa
a a
and doesn't match
aa
abc!
a&
Simplifying your allowed characters to be a-z and space for clarity, doesn't this do it?
^ *[a-z][a-z ]+[a-z] *$
Ignore spaces. Now a letter. Then some letters or spaces. Then a letter. Ignore more spaces.
The full thing becomes:
^ *[a-zA-Z0-9-'/\\&\.][a-zA-Z0-9-'/\\&\. ]+[a-zA-Z0-9-'/\\&\.] *$

asp.net mvc regular expression

(#"^\w+(?: \w+){0,8}$"
the above regular expression restrict all the special characters except _ . how would i restrict it.
Use
#"^[^\W_]+(?: [^\W_]+){0,8}$"
to allow everything that \w matches except _.
\W means "any character that isn't matched by \w", so by putting it into a negated character class and adding a _ to that class, we're effectively subtracting _ from \w.*
In other words, [^\W_] means "match any character that is neither a non-alphanumeric character nor an underscore".
Another way (perhaps more explicit and easier to understand) would be to use Unicode properties:
#"^[\p{L}\p{N}]+(?: [\p{L}\p{N}]+){0,8}$"
where [\p{L}\p{N}] means "any Unicode letter or number".
*In .NET, the \w shorthand matches a lot more than [A-Za-z0-9_], especially international (non-ASCII) letters.
Replace \w with [a-zA-Z0-9]. The shortcut \w matches any word character, i.e., alphanumeric characters and the underscore.
#"^[a-zA-Z0-9]+(?: [a-zA-Z0-9]+){0,8}$"
You can use [a-z0-9] once you figure out how to set the i flag (case-insensitive) on.

Resources