我需要在我的程序中使用正则表达式(C#),我无法找到好的Microsoft教程,有人可以帮我这个:
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
}
我如何将这个正则表达式(.1[4-5]iavi[0-9]{4}[A-z][A-z].bin
)写在内部if或者什么东西可以为我的这个函数提供服务。
由于
答案 0 :(得分:3)
您正在寻找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))
答案 1 :(得分:0)
正则表达式的.NET文档可在以下MSDN页面上找到: