C# 如何将字符串与现有正则表达式匹配?

C# 如何将字符串与现有正则表达式匹配?,c#,C#,我需要在我的程序(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 dictiona

我需要在我的程序(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

}
 var patterns = new[] {"pattern1", "pattern2", "pattern3"};

 return patterns.All(pattern => Regex.IsMatch(input, pattern))
我怎样才能在if中编写这个正则表达式(
.1[4-5]iavi[0-9]{4}[A-z][A-z].bin
),或者用它来实现这个函数的功能

谢谢

您正在寻找方法:

如果您有多个模式,并且希望确保字符串与所有模式匹配,则可以将它们存储到一个数组中,然后使用
Regex.IsMatch
Enumerable。all
方法如下:

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

}
 var patterns = new[] {"pattern1", "pattern2", "pattern3"};

 return patterns.All(pattern => Regex.IsMatch(input, pattern))
您正在寻找以下方法:

如果您有多个模式,并且希望确保字符串与所有模式匹配,则可以将它们存储到一个数组中,然后使用
Regex.IsMatch
Enumerable。all
方法如下:

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

}
 var patterns = new[] {"pattern1", "pattern2", "pattern3"};

 return patterns.All(pattern => Regex.IsMatch(input, pattern))
您正在寻找以下方法:

如果您有多个模式,并且希望确保字符串与所有模式匹配,则可以将它们存储到一个数组中,然后使用
Regex.IsMatch
Enumerable。all
方法如下:

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

}
 var patterns = new[] {"pattern1", "pattern2", "pattern3"};

 return patterns.All(pattern => Regex.IsMatch(input, pattern))
您正在寻找以下方法:

如果您有多个模式,并且希望确保字符串与所有模式匹配,则可以将它们存储到一个数组中,然后使用
Regex.IsMatch
Enumerable。all
方法如下:

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

}
 var patterns = new[] {"pattern1", "pattern2", "pattern3"};

 return patterns.All(pattern => Regex.IsMatch(input, pattern))

正则表达式的.NET文档可在以下MSDN页面上获得:


.NET正则表达式文档可在以下MSDN页面上找到:


.NET正则表达式文档可在以下MSDN页面上找到:


.NET正则表达式文档可在以下MSDN页面上找到:


.

检查和。检查和。检查和。检查和。我如何写我的图案?就用“”吗?因为我看到有人用@也。。。你能更具体地回答吗?@RonKah如果你的模式中有任何特殊字符,例如
“\b”
,你需要用
“\\b”
转义backslah,或者你可以使用
@“\b”
并使其成为逐字字符串。在您当前的模式中没有特殊字符,因此您可以直接编写。我如何编写我的模式?就用“”吗?因为我看到有人用@也。。。你能更具体地回答吗?@RonKah如果你的模式中有任何特殊字符,例如
“\b”
,你需要用
“\\b”
转义backslah,或者你可以使用
@“\b”
并使其成为逐字字符串。在您当前的模式中没有特殊字符,因此您可以直接编写。我如何编写我的模式?就用“”吗?因为我看到有人用@也。。。你能更具体地回答吗?@RonKah如果你的模式中有任何特殊字符,例如
“\b”
,你需要用
“\\b”
转义backslah,或者你可以使用
@“\b”
并使其成为逐字字符串。在您当前的模式中没有特殊字符,因此您可以直接编写。我如何编写我的模式?就用“”吗?因为我看到有人用@也。。。你能更具体地回答吗?@RonKah如果你的模式中有任何特殊字符,例如
“\b”
,你需要用
“\\b”
转义backslah,或者你可以使用
@“\b”
并使其成为逐字字符串。在当前模式中没有特殊字符,因此可以直接编写。