Reputation: 11155
I have a string, and I want to make sure that every letter in it is English. The other characters, I don't care.
34556#%42%$23$%^*&sdfsfr
- valid 34556#%42%$23$%^*&בלה בלה
- not validCan I do that with Linq? RegEx?
Thanks
Upvotes: 3
Views: 2574
Reputation: 92986
You can define in a character class either all characters/character ranges/Unicode-properties/blocks you want to allow or you don't want to allow.
[abc]
is a character class that allows a and b and c
[^abc]
is a negated character class that matches everything but not a or b or c
Here in your case I would go this way, no need to define every character:
^[\P{L}A-Za-z]*$
Match from the start to the end of the string everything that is not a letter [^\p{L}]
or A-Za-z
.
\p{L}
Is a Unicode property and matches everything that has the property letter. \P{L}
is the negated version, everything that is not a letter.
Test code:
string[] StrInputNumber = { "34556#%42%$23$%^*&sdfsfr", "asdf!\"§$%&/()=?*+~#'", "34556#%42%$23$%^*&בלה בלה", "öäü!\"§$%&/()=?*+~#'" };
Regex ASCIILettersOnly = new Regex(@"^[\P{L}A-Za-z]*$");
foreach (String item in StrInputNumber) {
if (ASCIILettersOnly.IsMatch(item)) {
Console.WriteLine(item + " ==> Contains only ASCII letters");
}
else {
Console.WriteLine(item + " ==> Contains non ASCII letters");
}
}
Some more basic regex explanations: What absolutely every Programmer should know about regular expressions
Upvotes: 4
Reputation: 61952
Maybe you could use
using System.Linq;
...
static bool IsValid(string str)
{
return str.All(c => c <= sbyte.MaxValue);
}
This considers all ASCII chars to be "valid" (even control characters). But punctuation and other special characters outside ASCII are not "valid". If str
is null
, an exception is thrown.
Upvotes: 3
Reputation: 176906
One thing you can try is put the char you want in this regx
bool IsValid(string input) {
return !(Regex.IsMatch(@"[^A-Za-z0-9'\.&@:?!()$#^]", input));
}
char other than specfied in the regx string are get ignored i.e return false..
Upvotes: 1