正则表达式匹配中的命名组
Named group in regular expression match
我正在尝试解析一些源文件以获得一些标准信息。
源文件可能如下所示:
// Name: BoltBait
// Title: Some cool thing
或
// Name :
// Title : Another thing
或
// Title:
// Name:
等等
我用来解析信息的代码如下所示:
Regex REName = new Regex(@"\/{2}\s*Name\s*:\s*(?<nlabel>.*)\n", RegexOptions.IgnoreCase);
Match mname = REName.Match(ScriptText); // entire source code file
if (mname.Success)
{
Name.Text = mname.Groups["nlabel"].Value.Trim();
}
如果该字段包含信息,则效果很好。如果该字段留空则不起作用。
例如,在上面的第三个示例中,Title
字段 return 匹配“// Name:”,我希望它 return 为空字符串。
我需要正则表达式专家的帮助。
我认为正则表达式太贪心了,所以我尝试了以下表达式:
@"\/{2}\s*Name\s*:\s*(?<nlabel>.*?)\n"
然而,这并没有帮助。
\s
包含换行符,此处不需要。
在 :
之后显式匹配制表符和空格应该就足够了
\/{2}\s*Name\s*:[\t ]*(?<nlabel>.*?)\n
这个 returns 空字符串在您的第三个示例中是正确的(名称和标题)。
我的方法是在非捕获组中使用替代项来匹配从冒号到行尾的标签。这匹配到行尾的任何内容,或者什么都不匹配。
var text1 = "// Name: BoltBait" + Environment.NewLine + "// Title: Some cool thing" + Environment.NewLine;
var text2 = "// Name :" + Environment.NewLine + "// Title : Another thing" + Environment.NewLine;
var text3 = "// Title:" + Environment.NewLine + "// Name:" + Environment.NewLine;
var texts = new List<string>() { text1, text2, text3 };
var options = RegexOptions.IgnoreCase | RegexOptions.Multiline;
var regex = new Regex("^//\s*?Name\s*?:(?<nlabel>(?:.*$|$))", options );
foreach (var text in texts){
var match = regex.Match( text );
Console.WriteLine( "|" + match.Groups["nlabel"].Value.Trim() + "|" );
}
生产:
|BoltBait|
||
||
您还可以使用 class subtraction 来避免匹配换行符:
//[\s-[\r\n]]*Name[\s-[\r\n]]*:[\s-[\r\n]]*(?<nlabel>.*)(?=\r?\n|$)
注意:
[\s-[\r\n]]*
- 匹配除换行符外的任何空格(使用字符 class 减法)
(?=\r?\n|$)
- 检查是否有换行符或字符串结尾的正向预测。
见regex demo,输出:
我正在尝试解析一些源文件以获得一些标准信息。
源文件可能如下所示:
// Name: BoltBait
// Title: Some cool thing
或
// Name :
// Title : Another thing
或
// Title:
// Name:
等等
我用来解析信息的代码如下所示:
Regex REName = new Regex(@"\/{2}\s*Name\s*:\s*(?<nlabel>.*)\n", RegexOptions.IgnoreCase);
Match mname = REName.Match(ScriptText); // entire source code file
if (mname.Success)
{
Name.Text = mname.Groups["nlabel"].Value.Trim();
}
如果该字段包含信息,则效果很好。如果该字段留空则不起作用。
例如,在上面的第三个示例中,Title
字段 return 匹配“// Name:”,我希望它 return 为空字符串。
我需要正则表达式专家的帮助。
我认为正则表达式太贪心了,所以我尝试了以下表达式:
@"\/{2}\s*Name\s*:\s*(?<nlabel>.*?)\n"
然而,这并没有帮助。
\s
包含换行符,此处不需要。
在 :
\/{2}\s*Name\s*:[\t ]*(?<nlabel>.*?)\n
这个 returns 空字符串在您的第三个示例中是正确的(名称和标题)。
我的方法是在非捕获组中使用替代项来匹配从冒号到行尾的标签。这匹配到行尾的任何内容,或者什么都不匹配。
var text1 = "// Name: BoltBait" + Environment.NewLine + "// Title: Some cool thing" + Environment.NewLine;
var text2 = "// Name :" + Environment.NewLine + "// Title : Another thing" + Environment.NewLine;
var text3 = "// Title:" + Environment.NewLine + "// Name:" + Environment.NewLine;
var texts = new List<string>() { text1, text2, text3 };
var options = RegexOptions.IgnoreCase | RegexOptions.Multiline;
var regex = new Regex("^//\s*?Name\s*?:(?<nlabel>(?:.*$|$))", options );
foreach (var text in texts){
var match = regex.Match( text );
Console.WriteLine( "|" + match.Groups["nlabel"].Value.Trim() + "|" );
}
生产:
|BoltBait|
||
||
您还可以使用 class subtraction 来避免匹配换行符:
//[\s-[\r\n]]*Name[\s-[\r\n]]*:[\s-[\r\n]]*(?<nlabel>.*)(?=\r?\n|$)
注意:
[\s-[\r\n]]*
- 匹配除换行符外的任何空格(使用字符 class 减法)(?=\r?\n|$)
- 检查是否有换行符或字符串结尾的正向预测。
见regex demo,输出: