我需要在我的程序(C#)中使用正则表达式,而且我找不到很好的微软教程,有人能帮我做这个吗?
1.这是我的表述:
.1[4-5]iavi[0-9]{4}[A-z][A-z].bin我想向函数发送一个字符串f,并且我想知道f是否与我所拥有的任何正则表达式相匹配,如果是这样的话,可以这样做:
void private Match(string f)
{
if( f is matching to the RegularExpression1)
add it to dictionary1..
if( f is matching to RegularExpression2)
add it to dictionary2
}我如何在if中编写这个正则表达式( .1[4-5]iavi[0-9]{4}[A-z][A-z].bin ),或者为我的函数服务的东西。
谢谢
发布于 2014-03-13 23:24:24
您正在寻找Regex.IsMatch方法:
bool isMatch = Regex.IsMatch(yourString, yourPattern);如果您有多个模式,并且希望确保您的字符串与所有模式匹配,则可以将它们存储到一个数组中,然后使用Regex.IsMatch和Enumerable.All方法,如下所示:
var patterns = new[] {"pattern1", "pattern2", "pattern3"};
return patterns.All(pattern => Regex.IsMatch(input, pattern))发布于 2014-03-13 23:28:20
正则表达式的.NET文档可在下面的MSDN页面上获得:
System.Text.RegularExpressions.Regex
正则表达式语言-快速引用。
https://stackoverflow.com/questions/22393060
复制相似问题