简体   繁体   中英

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 .

The method specification in Microsoft MSDN is:

public string Replace(
    string input,
    string replacement
)

In Visual Studio it is different:

视觉工作室

The second parameter is made to "support" Regex as says The Regular expression pattern to match

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 ?

In your example, pattern is a Regex.

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.

MSDN documentation for Regex.Mathc says:

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". It looks like you read the sentence as "... also accepting RegEx variable named pattern ".

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

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 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. 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. 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 . 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).

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 .

Note: The documentation matching the method overload you are looking at can be found here .

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.

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