I want to check for special characters in a string EXCEPT spaces and delete them.
Ex: input = "Oh Boy!!#$" output = "Oh Boy"
Can someone help me with the regular expression to implement this in C#
This is one way:
Console.WriteLine(Regex.Replace("Oh Boy!!#$", #"[^\w ]", ""));
Related
Have some imported data which is leaving me with little invalid character symbols such as:
Caf�
Just wondering what's the easiest way to find/replace these in string content?
var newString = yourString.Replace("�", "");
where yourString is Caf�.
The special character can be used in the Replace statement. It should be as simple as that.
This may help you. Results depend on what type of text you want to keep or remove...
MSDN: How to: Strip Invalid Characters from a String.
This will replace every nonalphanumeric characters(leaving punctuation intact):
string result = Regex.Replace(textBox1.Text, #"[^\w(\p{P}) ]+", "");
if you want only the letters and numbers and want to clear punctuation remove (\p{P}) from the expression.
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!
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);
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.
You can use below regex.
^[a-zA-Z0-9]+$
^[0-9a-zA-Z]+$
Matches one or more alphanumeric characters with no spaces or non-alpha characters.
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-##$##");
Try
^[a-zA-Z0-9]+$
www.regexr.com is a GREAT resource.
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.
I'm really a n00b when it comes to regular expressions. I've been trying to Split a string wherever there's a [----anything inside-----] for example.
string s = "Hello Word my name_is [right now I'm hungry] Julian";
string[] words = Regex.Split( s, "------");
The outcome would be "Hello Word my name_is " and " Julian"
The regex you want to use is:
Regex.Split( s, "\\[.*?\\]" );
Square brackets are special characters (specifying a character group), so they have to be escaped with a backslash. Inside the square brackets, you want any sequence of characters EXCEPT a close square bracket. There are a couple of ways to handle that. One is to specify [^\]]* (explicitly specifying "not a close square bracket"). The other, as I used in my answer, is to specify that the match is not greedy by affixing a question mark after it. This tells the regular expression processor not to greedily consume as many characters as it can, but to stop as soon as the next expression is matched.
#"\[.*?\]" will match the brackets of text
Another way to write it:
Regex.Split(str, #"\[[^]]*\]");
Is there any regular expression that will replace everything except alphanumeric?
My attempt (not working)
string str = "This is a string;;;; having;;; and It also 5555 777has dot (.) Many dots(.....)";
Regex rgx2 = new Regex("^[a-zA-Z0-9]+");
string result1 = rgx2.Replace(str, "");
[^a-zA-Z0-9]+ instead ^[a-zA-Z0-9]+
The ^ symbol in your second regex means 'at start of string', the way it is written. In order to have it negate the set it needs to be the first character after opening bracket:
[^a-zA-Z0-9]+
However, this will remove the - characters that you previously replaced spaces with. You probably want to exclude that character as well:
[^a-zA-Z0-9-]+
I have a string which has to be matching #"^[\w*$][\w\s-$]*((\d{1,})){0,1}$".
If it doesn't match this regular expression, I want the characters that do not match to be deleted from the string. How can I set this up?
s = Regex.Replace(s, #"^[^[\w*\$][\w\s-\$]*((\d{1,})){0,1}]$", "")
You probably want something like (but I am not sure of the actual question). Maybe you want to remove the whole regex if it does not match; that's not what the code below does:
s = Regex.Replace(s, #"^[^\w*\$]([\w*\$])[^\w*\$\s-]*([\w\s-\$]*).*$", "$1$2")
The idea is to interleave each wanted character blocks with list of forbidden characters and keep only those that you want. The end of your regex was a bit strange, so I simplified it.