c# - Delphi MatchesMask 函数的 C# 等价物是什么?

标签 c# delphi c#-4.0

相当于Delphi的MatchesMask是什么在 C# .NET 中的功能?我该如何使用它,我需要包含哪个 namespace 引用?

MatchesMask功能如帮助中所述:

Indicates whether a file name conforms to the format specified by a filter string.

Call MatchesMask to check the Filename parameter using the Mask parameter to describe valid values. A valid mask consists of literal characters, sets, and wildcards.

Each literal character must match a single character in the string. The comparison to literal characters is case-insensitive.

Each set begins with an opening bracket ([) and ends with a closing bracket (]). Between the brackets are the elements of the set. Each element is a literal character or a range. Ranges are specified by an initial value, a dash (-), and a final value. Do not use spaces or commas to separate the elements of the set. A set must match a single character in the string. The character matches the set if it is the same as one of the literal characters in the set, or if it is in one of the ranges in the set. A character is in a range if it matches the initial value, the final value, or falls between the two values. All comparisons are case-insensitive. If the first character after the opening bracket of a set is an exclamation point (!), then the set matches any character that is not in the set.

Wildcards are asterisks (*) or question marks (?). An asterisk matches any number of characters. A question mark matches a single arbitrary character.

MatchesMask returns true if the string matches the mask. MatchesMask returns false if the string does not match the mask. MatchesMask raises an exception if the mask is syntactically invalid.

Note: The Filename parameter does not need to be a file name. MatchesMask can be used to check strings against any syntactically correct mask.

最佳答案

现代编程语言提供正则表达式引擎,可以进行模式匹配。

C# 提供了 Regex 类,可以按以下方式使用:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        // First we see the input string.
        string input = "/content/alternate-1.aspx";

        // Here we call Regex.Match.
        Match match = Regex.Match(input, @"content/([A-Za-z0-9\-]+)\.aspx$",
            RegexOptions.IgnoreCase);

        // Here we check the Match instance.
        if (match.Success)
        {
            // Finally, we get the Group value and display it.
            string key = match.Groups[1].Value;
            Console.WriteLine(key);
        }
    }
}

您可以引用以下链接以快速引用

关于c# - Delphi MatchesMask 函数的 C# 等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11828908/

相关文章:

c#-4.0 - Unresolved 带有 SandcaSTLe 的装配引用

c# - 使用 Async/Await 时流正在关闭

c# - 内存中的二维数组

c# - 将函数作为参数传递并设置默认参数

delphi - 图表轴标题

c#-4.0 - 如何修改表达式以将其传递给方法

c# - EF Core Find 方法等效于多个记录?

delphi - 从 Delphi TISAPIApplication 获取端口/URL 数据 :

delphi - 使用 `TStopWatch` 而不使用 `free`

c# - 想要从字符串中删除双引号