Delphi MatchesMask函数的C#等效项是什么? [英] What is the C# equivalent of the Delphi MatchesMask function?

查看:75
本文介绍了Delphi MatchesMask函数的C#等效项是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Delphi的等效项是 MatchesMask C#.NET中的 函数?如何使用它,以及需要包含哪个名称空间引用?

What is the equivalent of the Delphi MatchesMask function in C# .NET ? How can I use it, and which namespace reference do I need to include ?

MatchesMask 函数,如帮助中所述:

The MatchesMask function as described in help:


指示文件名是否符合
过滤字符串指定的格式。

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

调用MatchesMask来检查Filename参数使用Mask
参数描述有效值。有效的掩码由文字
字符,集合和通配符组成。

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.

每个集合都以一个大括号([)开始,以一个大括号(b)结束( ])。括号之间是集合的元素。每个
元素都是文字字符或范围。范围由
初始值,破折号(-)和最终值指定。请勿使用空格或
逗号分隔集合中的元素。集合必须与字符串中的单个
字符匹配。如果该字符与该集合中的文字字符之一相同,或者该字符位于该集合范围中的一个
中,则该字符与该集合匹配。如果一个字符与
初始值,最终值匹配,或者介于两个值之间,则该字符在范围内。所有
比较都不区分大小写。如果集合中
开头括号后面的第一个字符是感叹号(!),则集合
会匹配集合中未包含的任何字符。

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如果字符串与掩码匹配,则返回true。如果字符串与掩码不匹配,则MatchesMask
返回false。如果掩码在语法上无效,则MatchesMask
会引发异常。

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.

注意:Filename参数不必是文件名。 MatchesMask可用于根据任何语法上正确的
正确掩码检查字符串。

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


推荐答案

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

Modern programming languages provide Regular Expression engines that allows to do matching of patterns.

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

C# provides Regex class that can be used in following manner:

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);
        }
    }
}

您可以参考以下链接

  • http://www.dotnetperls.com/regex-match (above example is from this site)
  • http://www.mikesdotnetting.com/Article/46/CSharp-Regular-Expressions-Cheat-Sheet (quick cheat sheet - quite handy)
  • http://msdn.microsoft.com/en-us/library/ms228595(v=vs.80).aspx (MS programming reference)

这篇关于Delphi MatchesMask函数的C#等效项是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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