C# 正则表达式通配符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15275718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:14:30  来源:igfitidea点击:

Regular expression wildcard

c#regexstringwildcard

提问by phadaphunk

I've just started using Regular Expressionsand this is so overwhelming that even after reading documentation I can't seem to find where to start to help with my problem.

我刚刚开始使用Regular Expressions,这太令人不知所措了,即使在阅读了文档之后,我似乎也找不到从哪里开始帮助解决我的问题。

I have to a bunch of strings.

我要一堆字符串。

 "Project1 - Notepad"
 "Project2 - Notepad"
 "Project3 - Notepad"
 "Untitled - Notepad"
 "HeyHo - Notepad"

And I have a string containing a wild card.

我有一个包含通配符的字符串。

"* - Notepad"

I would need that if I compare any of these strings with the one containing the wildcard it returns true. (With Regex.IsMatch()or something like that..)

如果我将这些字符串中的任何一个与包含通配符的字符串进行比较,我将需要它返回 true。(用Regex.IsMatch()或类似的东西..)

I don't usually asks for answers like that but I just can't find what I need. Could someone just point me out in the right direction ?

我通常不会要求这样的答案,但我就是找不到我需要的东西。有人可以指出我正确的方向吗?

采纳答案by Nolonar

The wildcard *is equivalent to the Regex pattern ".*"(greedy) or ".*?"(not-greedy), so you'll want to perform a string.Replace():

通配符*等效于正则表达式模式".*"(贪婪)或".*?"(非贪婪),因此您需要执行string.Replace()

string pattern = Regex.Escape(inputPattern).Replace("\*", ".*?");

Note the Regex.Escape(inputPattern)at the beginning. Since inputPatternmay contain special characters used by Regex, you need to properly escape those characters. If you don't, your pattern would explode.

注意Regex.Escape(inputPattern)开头。由于inputPattern可能包含 Regex 使用的特殊字符,您需要正确转义这些字符。如果你不这样做,你的模式就会爆炸。

Regex.IsMatch(input, ".NET"); // may match ".NET", "aNET", "FNET", "7NET" and many more

As a result, the wildcard *is escaped to \\*, which is why we replace the escaped wildcard rather than just the wildcard itself.

结果,通配符*被转义为\\*,这就是为什么我们替换转义的通配符而不仅仅是通配符本身。



To use the pattern

使用模式

you can do either:

你可以这样做:

Regex.IsMatch(input, pattern);

or

或者

var regex = new Regex(pattern);
regex.IsMatch(input);


Difference between greedy and not-greedy

贪心和不贪心的区别

The difference is in how much the pattern will try to match.

不同之处在于模式将尝试匹配多少。

Consider the following string: "hello (x+1)(x-1) world". You want to match the opening bracket (and the closing bracket )as well as anything in-between.

考虑以下字符串:"hello (x+1)(x-1) world". 您想要匹配左括号(和右括号)以及中间的任何内容。

Greedy would match only "(x+1)(x-1)"and nothing else. It basically matches the longest substring it can match.

Greedy 只会匹配而不匹配"(x+1)(x-1)"其他任何东西。它基本上匹配它可以匹配的最长子字符串。

Not-greedy would match "(x+1)"and (x-1)and nothing else. In other words: the shortest substrings possible.

不贪婪将匹配"(x+1)"(x-1)没有别的。换句话说:最短的子串可能。

回答by Daedalus

Seems like the pattern you want is the following:

看起来你想要的模式如下:

/^.+-\s*Notepad$/

This pattern will match an entire string if it ends with "- Notepad".

如果以“- Notepad”结尾,则此模式将匹配整个字符串。

回答by Alexander Forbes-Reed

I just wrote this quickly (based off of Validate that a string contain some exact words)

我只是快速写了这个(基于验证字符串包含一些确切的单词

    static void Main()
    {
        string[] inputs = 
        {
            "Project1 - Notepad", // True
            "Project2 - Notepad", // True
            "HeyHo - Notepad", // True
            "Nope - Won't work" // False
        };

        const string filterParam = "Notepad";
        var pattern = string.Format(@"^(?=.*\b - {0}\b).+$", filterParam);

        foreach (var input in inputs)
        {
            Console.WriteLine(Regex.IsMatch(input, pattern));
        }
        Console.ReadLine();
    }

回答by Thomas

You should do like this:

你应该这样做:

string myPattern = "* - Notepad";
foreach(string currentString in myListOfString)
    if(Regex.IsMatch(currentString, myPattern, RegexOptions.Singleline){
        Console.WriteLine("Found : "+currentString);
    }
}

By the way I saw you came from Montreal, additional french documentation + usefull tool: http://www.olivettom.com/?p=84

顺便说一下,我看到你来自蒙特利尔,额外的法语文档 + 有用的工具:http://www.olivettom.com/?p=84

Good luck!

祝你好运!