How do I check if a filename matches a wildcard pattern

前端 未结 8 1562
青春惊慌失措
青春惊慌失措 2020-11-30 06:16

I\'ve got a wildcard pattern, perhaps \"*.txt\" or \"POS??.dat\".

I also have list of filenames in memory that I need to compare to that pattern.

How would

8条回答
  •  失恋的感觉
    2020-11-30 07:03

    You can simply do this. You do not need regular expressions.

    using Microsoft.VisualBasic.CompilerServices;
    
    if (Operators.LikeString("pos123.txt", "pos?23.*", CompareMethod.Text))
    {
      Console.WriteLine("Filename matches pattern");
    }
    

    Or, in VB.Net,

    If "pos123.txt" Like "pos?23.*" Then
      Console.WriteLine("Filename matches pattern")
    End If
    

    In c# you could simulate this with an extension method. It wouldn't be exactly like VB Like, but it would be like...very cool.

提交回复
热议问题