删除(在C#中经常EX pression模式正则表达式的分隔符)与正则表达式的所有​​非单词字符 [英] Removing all non-word characters with regex (regex delimiters in C# regular expression pattern)

查看:138
本文介绍了删除(在C#中经常EX pression模式正则表达式的分隔符)与正则表达式的所有​​非单词字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的正则表达式:

I have a regex like this:

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

这正则表达式应该替换/, - ,。以。
不过,这并不可有人解释我为什么?

This regex should replace "/", "-", "." with "". But it doesn't, can someone explain me why?

推荐答案

不要使用正则表达式分隔符:

Do not use regex delimiters:

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

在C#中,您不能使用正则表达式的分隔符为语法来声明一个普通的前pression是从支持&LT PHP,Perl或JavaScript或他人的不同;作用> /<模式>(/< substituiton>)/修饰符正则表达式声明

In C#, you cannot use regex delimiters as the syntax to declare a regular expression is different from that of PHP, Perl or JavaScript or others that support <action>/<pattern>(/<substituiton>)/modifiers regex declaration.

只是为了避免术语混淆:内联改性剂的(执行不区分大小写的搜索,多行,单行,冗长和其他模式)的肯定支持,可用于代替相应的 RegexOptions 标志(虽然可能 RegexOptions 标志的数目比内联改性剂更高)。尽管如此,正则表达式的分隔符的不影响正则表达式模式可言,它们只是声明语法的一部分,不影响模式本身。我说,他们只是一种替代了; 或换行分隔code行

Just to avoid terminology confusion: inline modifiers (enforcing case-insensitive search, multiline, singleline, verbose and other modes) are certainly supported and can be used instead of the corresponding RegexOptions flags (though the number of possible RegexOptions flags is higher than that of inline modifiers). Still, regex delimiters do not influence the regex pattern at all, they are just part of declaration syntax, and do not impact the pattern itself. Say, they are just kind of substitutes for ; or newline separating lines of code.

在C#中,正则表达式定界符是没有必要的,因而不被支持。 Perl样式 S / \\ W //摹将被写成 VAR替换= Regex.Replace(STR,@\\ W字符串。空); 。等等。

In C#, regex delimiters are not necessary and thus are not supported. Perl-style s/\W//g will be written as var replaced = Regex.Replace(str, @"\W", string.Empty);. And so on.

这篇关于删除(在C#中经常EX pression模式正则表达式的分隔符)与正则表达式的所有​​非单词字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆