包含正则表达式分隔符的简单且经过测试的在线正则表达式在 C# 代码中不起作用 [英] Simple and tested online regex containing regex delimiters does not work in C# code

查看:20
本文介绍了包含正则表达式分隔符的简单且经过测试的在线正则表达式在 C# 代码中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的正则表达式:

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# 中,您不能使用正则表达式分隔符,因为声明正则表达式的语法不同于 PHP、Perl 或 JavaScript 或其他支持 /(/;)/modifiers 正则表达式声明.

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 标志的数量高于内联修饰符的数量).尽管如此,regex delimiters 根本不影响正则表达式模式,它们只是声明语法的一部分,不会影响模式本身.比如说,它们只是 ; 或分隔代码行的换行符的替代品.

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//g 将被写成 var Replaced = Regex.Replace(str, @"W", string.Empty);.等等.

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# 代码中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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