代码之家  ›  专栏  ›  技术社区  ›  dlras2

Regex在不同的计算机上工作方式不同

  •  1
  • dlras2  · 技术社区  · 15 年前

    const string _pattern
        = @"^(?:\x02)?([A-Z])(ST)([WS])([1-9])([ AM])([ NSEWIO])([- ]\d{6})([ M])([1CPN])(\w)?(?:\x0D)?$";
    static readonly Regex _regex
        = new Regex(_pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
    

    字符串:

    @"ASTS2MI-000020 C0"
    
    1 回复  |  直到 15 年前
        1
  •  6
  •   Matthew Flaschen    15 年前

    可能您需要文化变量:

    static readonly Regex _regex
        = new Regex(_pattern, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled);
    

    正如在 Performing Culture-Insensitive Operations in the RegularExpressions Namespace ,默认情况下,不区分大小写的匹配会考虑 Thread.CurrentCulture .

    推荐文章