I have this
regex Regex.Replace(listing.Company, @"[^A-Za-z0-9_\.~]+", "-");
listing.Company is a string, this works but when a string has dots it does not remove them.
Could you please help me out
In your current regex, you have \\.
in your exclusion, which will cause it to be ignored by Regex.Replace
. Also, your regex does nothing to convert the input string to lower case. You can do that afterwards, but doing it before your Replace makes your pattern simpler.
Try this method out:
var output = Regex.Replace(listing.Company.ToLower(), "[^a-z0-9_]+", "-");
try
Regex.Replace(listing.Company.ToLower(), @"[^a-z0-9_]+", "-");
you are excluding \\.
which is for dot. Also, if you want it in lower letters, you need to convert the string to lower case first.
Perhaps you are looking for something like this:
string res = Regex.Replace(listing.Company, @"[\W+\.~]", "-");
Here regex engine will look for any character other than AZ, az, underscore
along with dot
and ~
and will replace it with "-".
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.