我正在查看此代码,它正在使用此函数格式化所有正则表达式:
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?\})$");
答案 0 :(得分:5)
这不是正则表达式 - 它是一个格式字符串,因为这是对string.Format
的调用。
这只是格式化字符串并将regex
变量的值(或者更确切地说是调用ToString()
的结果)放在{0}
上。
结果是字符串"(?-mix:<whatever regex.ToString() is>)"
。
这个字符串看起来像它可能是一个正则表达式,并会关闭一些修饰符(所以这将区分大小写,^和$匹配开始和仅行结束和自由间距模式已关闭)。请参阅Regular Expression Advanced Syntax Reference上的www.regular-expressions.info。
因此,以上内容会将regex
与这些选项匹配。
答案 1 :(得分:0)
嗯,有点晚了,但万一你还没知道它意味着什么:
正则表达式定义了(
和)
之间的捕获组。当您需要分组但非捕获分组时,您将在(?:
和)
之间编写表达式。并且一些正则表达式处理器接受非捕获组的?
和:
之间的开/关标志。所以有&#34;混合&#34;东西,实际上意味着关闭一些关于组的标志,用-
标志分隔(关闭标志):
m modifier off
: - 多行,^和$匹配不仅仅是行的开头/结尾i modifier off
: - 不敏感,区分大小写的匹配x modifier off
:-extended,模式中的空格是文字空格因此,当您将其关闭时,它会变为(?-mix:
... )
。与(?-ixm:
... )
或任何其他顺序相同。
无论如何,我不认为.NET的Regex类关心那些标志,以后再检查一下。它是从原始的红宝石液体发动机源迁移而来的。