简体   繁体   English

我认为我在 C# 中的正则表达式模式不正确

[英]I think my regular expression pattern in C# is incorrect

I'm checking to see if my regular expression matches my string.我正在检查我的正则表达式是否与我的字符串匹配。 I have a filename that looks like somename_somthing.txt and I want to match it to somename_*.txt , but my code is failing when I try to pass something that should match.我有一个看起来像somename_somthing.txt的文件名,我想将它与somename_*.txt匹配,但是当我尝试传递应该匹配的内容时,我的代码失败了。 Here is my code.这是我的代码。

string pattern = "somename_*.txt";
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
using (ZipFile zipFile = ZipFile.Read(fullPath))
{
   foreach (ZipEntry e in zipFile)
   {
       Match m = r.Match("somename_something.txt");
       if (!m.Success)
       {
           throw new FileNotFoundException("A filename with format: " + pattern + " not found.");
        }
    }
}

In General一般来说

Regex give in this code matches the _ with an * meaning zero or more underscores instead of what you intended.此代码中的正则表达式将 _ 与 * 匹配,表示零个或多个下划线而不是您想要的。 The * is used to denote zero or more of the previous item. * 用于表示前一项的零个或多个。 Instead try而是尝试

^somename_(.*)\.txt$

This matches exactly the first part "somename_".这与第一部分“somename_”完全匹配。

Then anything (.*)然后任何 (.*)

And finally the end ".txt".最后是“.txt”。 The backslash escapes the 'dot'.反斜杠转义“点”。

More Specific更加具体

You can also say if you only want letters and not numbers or symbols in the middle part of the match with:您还可以说是否在匹配的中间部分只想要字母而不是数字或符号:

^somename_[a-z]*\.txt$

The asterisk is matching the underscore and throwing it off.星号匹配下划线并将其丢弃。

Try:尝试:

somename_(\w+).txt

The (\\w+) here will match the group at this location.此处的 (\\w+) 将匹配此位置的组。

You can see it match here: https://regex101.com/r/qS8wA5/1你可以在这里看到它匹配: https : //regex101.com/r/qS8wA5/1

As written, your regular expression正如所写,您的正则表达式

somename_*.txt

matches (in a case-insensitive manner):匹配(以不区分大小写的方式):

  • the literal text somename , followed by文字somename ,后跟
  • zero or more underscore characters ( _ ), followed零个或多个下划线字符 ( _ ),后跟
  • any character (other than newline), followed任何字符(换行符除外),后跟
  • the literal text txt文字文本txt

And it will match that anywhere in the source text.它将匹配源文本中的任何地方 You probably want to write something like你可能想写一些类似的东西

Regex myPattern = new Regex( @"
    ^        # anchor the match to start-of-text, followed by
    somename # the literal 'somename', followed by
    _        # a literal underscore character, followed by
    .*       # zero or of any character (except newline), followed by
    \.       # a literal period/fullstop, followed by
    txt      # the literal text 'txt' 
    $        # with the match anchored at end-of-text
  " , RegexOptions.IgnoreCase|RegexOptions.IgnorePatternWhitespace
  ) ;

Hi I think the pattern should be嗨,我认为模式应该是

string pattern = "somename_.*\\.txt";

Regards问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM