Skip to main content
 首页 » 编程设计

c#之以下正则表达式在做什么 (-mix :{0})

2024年02月02日30soundcode

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

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?\})$"); 

请您参考如下方法:

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

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

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

这个字符串看起来可能是一个正则表达式,并且会关闭一些修饰符(因此这将区分大小写,^ 和 $ 仅匹配行的开始和结束以及自由间距模式已关闭)。参见 Regular Expression Advanced Syntax Reference在 www.regular-expressions.info .

所以上面会匹配 regex关闭这些选项。