1

我正在查看这段代码,它正在使用此函数格式化所有正则表达式:

string.Format("(?-mix:{0})", regex);

究竟是什么(?-mix:{0})意思?(我知道这{0}是一个占位符)。

这是代码:

https://github.com/formosatek/dotliquid/blob/master/src/DotLiquid/Liquid.cs#L36 https://github.com/formosatek/dotliquid/blob/master/src/DotLiquid/Util/R.cs #L12

public static string Q(string regex)
        {
            return string.Format("(?-mix:{0})", regex);
        }


public static class Liquid
    {
        internal static readonly ResourceManager ResourceManager = new ResourceManager(typeof(DotLiquid.Properties.Resources));

        public static readonly string FilterSeparator = R.Q(@"\|");
        public static readonly string ArgumentSeparator = R.Q(@",");
        public static readonly string FilterArgumentSeparator = R.Q(@":");
        public static readonly string VariableAttributeSeparator = R.Q(@".");
        public static readonly string TagStart = R.Q(@"\{\%");
        public static readonly string TagEnd = R.Q(@"\%\}");
        public static readonly string VariableSignature = R.Q(@"\(?[\w\-\.\[\]]\)?");
        public static readonly string VariableSegment = R.Q(@"[\w\-]");
        public static readonly string VariableStart = R.Q(@"\{\{");
        public static readonly string VariableEnd = R.Q(@"\}\}");
        public static readonly string VariableIncompleteEnd = R.Q(@"\}\}?");
        public static readonly string QuotedString = R.Q(@"""[^""]*""|'[^']*'");
        public static readonly string QuotedFragment = string.Format(R.Q(@"{0}|(?:[^\s,\|'""]|{0})+"), QuotedString);
        public static readonly string QuotedAssignFragment = string.Format(R.Q(@"{0}|(?:[^\s\|'""]|{0})+"), QuotedString);
        public static readonly string StrictQuotedFragment = R.Q(@"""[^""]+""|'[^']+'|[^\s\|\:\,]+");
        public static readonly string FirstFilterArgument = string.Format(R.Q(@"{0}(?:{1})"), FilterArgumentSeparator, StrictQuotedFragment);
        public static readonly string OtherFilterArgument = string.Format(R.Q(@"{0}(?:{1})"), ArgumentSeparator, StrictQuotedFragment);
        public static readonly string SpacelessFilter = string.Format(R.Q(@"^(?:'[^']+'|""[^""]+""|[^'""])*{0}(?:{1})(?:{2}(?:{3})*)?"), FilterSeparator, StrictQuotedFragment, FirstFilterArgument, OtherFilterArgument);
        public static readonly string Expression = string.Format(R.Q(@"(?:{0}(?:{1})*)"), QuotedFragment, SpacelessFilter);
        public static readonly string TagAttributes = string.Format(R.Q(@"(\w+)\s*\:\s*({0})"), QuotedFragment);
        public static readonly string AnyStartingTag = R.Q(@"\{\{|\{\%");
        public static readonly string PartialTemplateParser = string.Format(R.Q(@"{0}.*?{1}|{2}.*?{3}"), TagStart, TagEnd, VariableStart, VariableIncompleteEnd);
        public static readonly string TemplateParser = string.Format(R.Q(@"({0}|{1})"), PartialTemplateParser, AnyStartingTag);
        public static readonly string VariableParser = string.Format(R.Q(@"\[[^\]]+\]|{0}+\??"), VariableSegment);
        public static readonly string LiteralShorthand = R.Q(@"^(?:\{\{\{\s?)(.*?)(?:\s*\}\}\})$");
        public static readonly string CommentShorthand = R.Q(@"^(?:\{\s?\#\s?)(.*?)(?:\s*\#\s?\})$");
4

2 回答 2

5

这不是正则表达式 - 它是格式字符串,因为这是对string.Format.

这只是格式化字符串并将regex变量的值(或者更确切地说是调用ToString()它的结果)放置在{0}.

结果是字符串"(?-mix:<whatever regex.ToString() is>)"

这个字符串看起来可能是一个正则表达式,并且会关闭一些修饰符(因此这将区分大小写,^ 和 $ 仅匹配行首和行尾,并且关闭自由间距模式)。请参阅www.regular-expressions.info上的正则表达式高级语法参考

因此,上述内容将regex与这些选项相匹配。

于 2012-10-11T15:16:21.670 回答
1

好吧,有点晚了,但以防万一你没有发现它的含义:

正则表达式在(和之间定义一个捕获组)。当您需要分组但非捕获分组时,您将编写介于(?:and之间的表达式)。一些正则表达式处理器接受非捕获组之间的?开/关标志。:所以有“混合”的东西,这实际上意味着关闭一些关于组的标志,用-符号分隔(关闭标志):

  • m modifier off: -multi-line, ^ 和 $ 不仅匹配行的开始/结束
  • i modifier off: -insensitive,区分大小写的匹配
  • x modifier off: -extended,模式中的空格是文字空格

所以,当你把它们关掉时,它就变成了(?-mix:…… 。)(?-ixm:...)或任何其他顺序相同。

无论如何,我认为 .NET 的 Regex 类并不关心这些标志,最好稍后再检查。它是从液体引擎的原始红宝石源迁移而来的。

于 2015-02-15T16:06:25.560 回答