Removing all non-capital characters using regular expressions (regular expressions in the C # regular expression pattern)

I have a regex:

name = dr-det-fb.ydp.eu/ebook/trunk/annotations/ctrl.php/api1751-4060-1193-0487
name = Regex.Replace(name, @"/\W/g", "");

This regular expression should replace "/", "-", "." with "". But this is not so, can someone explain to me why?

+2
source share
1 answer

Do not use regular expression delimiters:

name = Regex.Replace(name, @"\W", "");

In C #, you cannot use regular expression delimiters because the syntax for declaring a regular expression is different from PHP, Perl or JavaScript, or others that support <action>/<pattern>(/<substituiton>)/modifiersregex declarations .

, : ( , , , ), , RegexOptions ( RegexOptions , ). , , . , ; , .

# , , . Perl- s/\W//g var replaced = Regex.Replace(str, @"\W", string.Empty);. .

+3

Source: https://habr.com/ru/post/1653320/


All Articles