Reputation: 3617
I'm a real regex n00b so I ask your help: I need a regex witch match only letters and numbers and exclude punctations, non ascii characters and spaces.
"ilikestackoverflow2012" would be a valid string.
"f### you °§è" not valid.
"hello world" not valid
"hello-world" and "*hello_world*" not valid
and so on.
I need it to make a possibly complex business name url friendly.
Thanks in advance!
Upvotes: 3
Views: 4486
Reputation: 3718
^[0-9a-zA-Z]+$
Matches one or more alphanumeric characters with no spaces or non-alpha characters.
Upvotes: 2
Reputation: 838216
To validate a string you can use the following regular expression with Regex.IsMatch
:
"^[0-9A-Za-z]+$"
Explanation:
^
is a start of string anchor.[...]
is a character class.+
means one or more.$
is an end of string anchor.I need it to make a possibly complex business name url friendly
Then you want to replace the characters that don't match. Use Regex.Replace
with the following regular expression:
"[^0-9A-Za-z]+"
Explanation:
[^...]
is a negated character class.Code:
string result = Regex.Replace(input, "[^0-9A-Za-z]+" , "");
See it working online: ideone
Note that different business names could give the same resulting string. For example, businesses whose names contain only Chinese characters will all give the empty string.
Upvotes: 2
Reputation: 116118
You don't need regex for this.
string s = "......"
var isValid = s.All(Char.IsLetterOrDigit);
-
I need it to make a possibly complex business name url friendly
You can also use HttpUtility.UrlEncode
var urlFriendlyString = HttpUtility.UrlEncode(yourString);
Upvotes: 2
Reputation: 9497
Try this:
var regex = new Regex(@"^[a-zA-Z0-9]+$");
var test = new[] {"ilikestack", "hello world", "hello-world", "###"};
foreach (var s in test)
Console.WriteLine("{0}: {1}", s, regex.IsMatch(s));
EDIT: If you want something like @Andre_Miller said, you should use the same regex with Regex.Replace();
Regex.Replace(s, @"[^a-zA-Z0-9]+", "")
OR
var regex = new Regex(@"^[a-zA-Z0-9]+$");
regex.Replace("input-string-@#$@#");
Upvotes: 1
Reputation: 14900
What's wrong with [:alnum:]
? It's a posix standard. So your whole regex would be: ^[:alnum:]+$
.
The wikipedia article for regular expressions includes lots of examples and details.
Upvotes: 1