C# 下面的正则表达式在做什么(?-mix:{0})
我正在看这段代码,它使用以下函数格式化所有正则表达式:C# 下面的正则表达式在做什么(?-mix:{0}),c#,regex,C#,Regex,我正在看这段代码,它使用以下函数格式化所有正则表达式: string.Format("(?-mix:{0})", regex); (?-mix:{0})的确切含义是什么?(我知道{0}是一个占位符) 代码如下: https://github.com/formosatek/dotliquid/blob/master/src/DotLiquid/Util/R.cs#L12 这不是正则表达式-它是一个格式字符串,因为这是对string.format的调用 这只是格式化字符串并将regex变量的值(
string.Format("(?-mix:{0})", regex);
(?-mix:{0})
的确切含义是什么?(我知道{0}
是一个占位符)
代码如下:
https://github.com/formosatek/dotliquid/blob/master/src/DotLiquid/Util/R.cs#L12
这不是正则表达式-它是一个格式字符串,因为这是对
string.format
的调用
这只是格式化字符串并将regex
变量的值(或者更确切地说是对其调用ToString()
的结果)放置在{0}
的位置
结果是字符串“(?-mix:)”
此字符串看起来可能是正则表达式,并将关闭一些修饰符(因此这将区分大小写,^和$match start and end of line only,且自由间距模式已关闭)。再见
因此,在关闭这些选项的情况下,上面将匹配
regex
。这不是一个正则表达式-它是一个格式字符串,因为这是对string.format
的调用
这只是格式化字符串并将regex
变量的值(或者更确切地说是对其调用ToString()
的结果)放置在{0}
的位置
结果是字符串“(?-mix:)”
此字符串看起来可能是正则表达式,并将关闭一些修饰符(因此这将区分大小写,^和$match start and end of line only,且自由间距模式已关闭)。再见
因此,在关闭这些选项的情况下,上面将匹配
regex
。好吧,有点晚了,但以防万一您没有发现它的含义:
正则表达式定义了(
和)
之间的捕获组。当您需要分组,但不是捕获分组时,您将在(?:
和)
之间编写表达式。一些正则表达式处理器接受非捕获组的?
和:
之间的开/关标志。“混合”的意思是关闭组的一些标志,用-
符号分隔(关闭标志):
:-多行,^和$match不仅仅是行的开始/结束m modifier off
:-不区分大小写,匹配i修饰符关闭
:-扩展,模式中的空白是文字空白x修饰符关闭
(?-mix:
…)
。这与(?-ixm:
…)
或任何其他顺序都是一样的
无论如何,我不认为.NET的正则表达式类关心这些标志,最好稍后再检查。它是从liquid engine的原始ruby源迁移而来的。好吧,有点晚了,但以防万一您没有发现它的含义: 正则表达式定义了
(
和)
之间的捕获组。当您需要分组,但不是捕获分组时,您将在(?:
和)
之间编写表达式。一些正则表达式处理器接受非捕获组的?
和:
之间的开/关标志。“混合”的意思是关闭组的一些标志,用-
符号分隔(关闭标志):
:-多行,^和$match不仅仅是行的开始/结束m modifier off
:-不区分大小写,匹配i修饰符关闭
:-扩展,模式中的空白是文字空白x修饰符关闭
(?-mix:
…)
。这与(?-ixm:
…)
或任何其他顺序都是一样的
无论如何,我不认为.NET的正则表达式类关心这些标志,最好稍后再检查。它是从liquid engine的原始ruby源迁移而来的。结果字符串用于什么?它用于传递到正则表达式中。Match结果字符串用于什么?它用于传递到正则表达式中。Match我的问题是关于正则表达式的,它基本上是使用此字符串包装所有正则表达式。Format,所以我想问一下,-mix:part在做什么。@user1361315-你发布的代码把事情弄糊涂了,因为没有使用
Regex
。但是,我已经更新了一些详细的答案。我的问题是关于正则表达式的,它基本上是使用这个字符串.Format包装所有使用的正则表达式,所以我想知道-mix:part在做什么。@user1361315-您发布的代码混淆了这一点,因为没有使用regex
。但是,我已经用一些细节更新了答案,.NET关心并支持这个符号,(?imnsx imnsx:subexpression)
。来源:.NET关心并支持这种表示法,(?imnsx imnsx:subexpression)
。资料来源:
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?\})$");