简体   繁体   English

为什么C#Regex.Match不接受Regex类型作为第二个参数?

[英]Why C# Regex.Match doesn't accept type Regex as second argument?

I wonder why does the static method Match in Regex.Match receive two obligatory parameters and more optionals and the second parameter doesn't accept a true Regex . 我想知道为什么Regex.Match中的静态方法Match接收两个强制性参数和更多可选参数,而第二个参数却不接受真正的Regex

The method specification in Microsoft MSDN is: Microsoft MSDN中的方法规范为:

public string Replace(
    string input,
    string replacement
)

In Visual Studio it is different: 在Visual Studio中,它是不同的:

视觉工作室

The second parameter is made to "support" Regex as says The Regular expression pattern to match 第二个参数用于“支持” Regex, 如正则表达式模式以匹配

Then, the following code is invalid: 然后,以下代码无效:

  string str = "my {value}";
  Regex pattern = new Regex(@"\{[a-zA-Z_][a-zA-Z0-9_]*\}");
  int matches = Regex.Match(str, pattern);

But when: 但当:

string pattern = @"\{[a-zA-Z_][a-zA-Z0-9_]*\}";

It is valid. 是有效的。

Am I getting crazy or this is really a issue? 我会发疯还是这真的是一个问题?

I know, it says that receives "string", but wouldn't be correctly also support type Regex ? 我知道,它说接收“字符串”,但是也不能正确地支持Regex类型吗?

In your example, pattern is a Regex. 在您的示例中, pattern 一个正则表达式。

You probably want: 您可能想要:

var matches = pattern.Match(str);

the reason the static version exists, is that it adds a quick way to match a regular expression in a one-off, disposable way. 静态版本存在的原因是,它以一种一次性的一次性方式添加了一种快速匹配正则表达式的方法。

Since Regex's internal state machines can take time to compile - the instance version exists so you can create an instance - and only have to compile it one time - for example, if you were running it many times within a loop, you could see a considerable performance improvement. 由于Regex的内部状态机可能需要花费一些时间来编译-实例版本存在,因此您可以创建一个实例-只需编译一次即可-例如,如果您在一个循环中多次运行它,则可能会看到大量性能改进。

MSDN documentation for Regex.Mathc says: Regex.Mathc的 MSDN文档说:

pattern
   Type: System.String
   The regular expression pattern to match. 

"The regular expression pattern to match." “要匹配的正则表达式模式。” means "String representation of regular expression to match, with possible extensions to usual regular expression syntax supported by .Net runtime". 表示“要匹配的正则表达式的字符串表示形式,并可能扩展到.Net运行时支持的常规正则表达式语法”。 It looks like you read the sentence as "... also accepting RegEx variable named pattern ". 好像您将句子读为“ ...还接受名为pattern RegEx变量”。

Note that regular expression is normal computer science / language theory term and does not have to match any particular class in any framework. 请注意, 正则表达式是正常的计算机科学/语言理论术语,不必与任何框架中的任何特定类匹配。

While it may be useful to add "String with the regular expression", it does not feel very useful because type is shown immediately before the sentence. 尽管添加“带正则表达式的字符串”可能很有用,但由于类型显示在句子的紧前面,因此感觉不是很有用。

I don't think it is entirely clear what you are asking, but I will have a go at answering part of your question anyway. 我不完全清楚您要问的是什么,但是无论如何我都会去回答您的部分问题。

The second parameter is made to "support" Regex as says The Regular expression pattern to match 第二个参数用于“支持” Regex,如正则表达式模式以匹配

If I understand you correctly, you are asking why you cannot pass a Regex when the description of the parameter says that it takes a regular expression. 如果我对您的理解正确,那么您会问为什么当参数说明说它采用了正则Regex时为什么不能传递正则表达式。

Regex vs. regular expressions 正则表达式与正则表达式

It is important here to distinguish between the .NET type Regex and the concept of a regular expression: Regex is a .NET type just like Color , String , StringBuilder etc. It is designed to represent a regular expression and it has convenient methods for working with regular expressions. 在这里重要的是要区分.NET类型Regex和正则表达式的概念: Regex是.NET类型,就像ColorStringStringBuilder等。它旨在表示正则表达式,并且具有方便的工作方法与正则表达式。 However, it isn't in itself a regular expression. 但是,它本身不是正则表达式。

On the other hand, the string of characters "abc.*" is a regular expression , not just in C# but in a general sense. 另一方面,字符串“ abc。*”是一个正则表达式 ,不仅在C#中,而且在一般意义上。 Not all strings are valid regular expressions, but some strings are regular expressions and can be used to describe a universe of matching strings. 并非所有的字符串都是有效的正则表达式,但是某些字符串是正则表达式,可用于描述匹配字符串的范围。

Method signatures 方法签名

What the documentation above states is that the method takes a parameter, pattern , of type string . 上面的文档指出的是,该方法采用string类型的参数pattern The comments associated with the parameter state that the pattern must represent a valid regular expression. 与参数关联的注释指出模式必须代表有效的正则表达式。 Thus, it must conform to the rules describing regular expressions in .NET and cannot, say, contain "?[---]<>", because that string does not represent a regular expression (I assume, I haven't tested it). 因此,它必须符合描述.NET中正则表达式的规则,并且不能包含“?[---] <>”,因为该字符串不代表正则表达式(我假设,我尚未对其进行测试。 )。

To sum up: you cannot pass an instance of the type Regex for the pattern parameter, because the method is not asking for an instance of Regex , it is very explicitly asking for a regular expression expressed as a string . 综上所述:您不能为模式参数传递Regex类型的实例,因为该方法并不要求Regex的实例,而是非常明确地要求以string形式表示的正则表达式

Note: The documentation matching the method overload you are looking at can be found here . 注意:可在此处找到与您正在查看的方法重载匹配的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM