string Val = Regex.Replace(TextBox1.Text, @"[^a-z, A-z, 0-9]", string.Empty);
This expression does not match the character ^ and _ . What should i do to match those values?
One more things is, If TextBox1.Text string value is more than 10, the last string value(11th string value) should match.
Note that the ^ is has special meaning when enclosed in square brackets. It means match everything but those specified in the character class, basically '[]'
.
If you want to match "^"
and "_"
, put the caret (^)
in another position than after the opening bracket like so, using the repetition to restrict character length:
[\W_]
That will make sure the characters in the entire string are 10.
Or you escape it using the slash "\\^"
.
string Val = Regex.Replace(TextBox1.Text, @"[\W_]", string.Empty);
Your problem is Az
.
This matches all ASCII letters A
through Z
, then the characters that lie between Z
and a
(which contain, among others, ^
and _
) , then all ASCII letters between a
and z
. This means that ^
and _
won't be matched by your regex (as well as the comma and space which you included in your regex as well).
To clarify, your regex could also have been written as
[^a-zA-Z0-9\[\\\]^_` ,]
You probably wanted
string Val = Regex.Replace(TextBox1.Text, @"[^a-zA-Z0-9]", string.Empty);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.