C# 中的 RegEx 捕获组
RegEx Capturing Groups in C#
我正在尝试使用正则表达式解析文件的内容,但我想出的模式似乎不起作用。
我尝试使用的正则表达式是
string regex = @"^(?<key>\w+?)\s*?:\s*?{(?<value>[\s\S]+?)}$";
我试图解析的文本是
string text = @"key:{value}
key:{valu{0}e}
key:{valu
{0}e}
key:{val-u{0}e}
key:{val__[!]
-u{0}{1}e}";
但是,returns 0 个结果
MatchCollection matches = Regex.Matches(text, regex, RegexOptions.Multiline);
我已经尝试在 RegExr 上测试这个正则表达式,它按预期工作。
我不确定为什么这在 C# 中尝试时不起作用。
MCVE:
string regex = @"^(?<key>\w+?)\s*?:\s*?{(?<value>[\s\S]+?)}$";
string text = @"key:{value}
key:{valu{0}e}
key:{valu
{0}e}
key:{val-u{0}e}
key:{val__[!]
-u{0}{1}e}";
MatchCollection matches = Regex.Matches(text, regex, RegexOptions.Multiline);
Console.WriteLine(matches.Count);
我们可能想尝试的一种方法是测试我们的表达式是否适用于另一种语言。
此外,我们可能想简化表达式:
^(.*?)([\s:]+)?{([\s\S].*)?.$
我们有三个捕获组。第一个和第三个是我们想要的键和值。
正则表达式
您可以 modify/simplify/change 在 regex101.com 中表达您的表情。
正则表达式电路
您还可以在 jex.im:
中可视化您的表情
JavaScript 演示
const regex = /^(.*?)([\s:]+)?{([\s\S].*)?.$/gm;
const str = `key:{value}
key:{valu{0}e}
key:{valu
{0}e}
key: {val-u{0}e}
key: {val__[!]
-u{0}{1}e}`;
const subst = `,`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
C# 测试
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"^(.*?)([\s:]+)?{([\s\S].*)?.$";
string substitution = @",";
string input = @"key:{value}
key:{valu{0}e}
key:{valu
{0}e}
key: {val-u{0}e}
key: {val__[!]
-u{0}{1}e}";
RegexOptions options = RegexOptions.Multiline;
Regex regex = new Regex(pattern, options);
string result = regex.Replace(input, substitution);
}
}
原始正则表达式测试
const regex = /^(?<key>\w+?)\s*?:\s*?{(?<value>[\s\S]+?)}$/gm;
const str = `key:{value}
key:{valu{0}e}
key:{valu
{0}e}
key: {val-u{0}e}
key: {val__[!]
-u{0}{1}e}`;
const subst = `,`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
参考文献:
- String.replaceAll without RegEx
- C# regular expression match
- Regular Expression Groups in C#
- c# regex matches example
- Regex: How to get a group name
- How to use inline modifiers in C# regex?
- Regex to first match, then replace found matches
我正在尝试使用正则表达式解析文件的内容,但我想出的模式似乎不起作用。
我尝试使用的正则表达式是
string regex = @"^(?<key>\w+?)\s*?:\s*?{(?<value>[\s\S]+?)}$";
我试图解析的文本是
string text = @"key:{value}
key:{valu{0}e}
key:{valu
{0}e}
key:{val-u{0}e}
key:{val__[!]
-u{0}{1}e}";
但是,returns 0 个结果
MatchCollection matches = Regex.Matches(text, regex, RegexOptions.Multiline);
我已经尝试在 RegExr 上测试这个正则表达式,它按预期工作。
我不确定为什么这在 C# 中尝试时不起作用。
MCVE:
string regex = @"^(?<key>\w+?)\s*?:\s*?{(?<value>[\s\S]+?)}$";
string text = @"key:{value}
key:{valu{0}e}
key:{valu
{0}e}
key:{val-u{0}e}
key:{val__[!]
-u{0}{1}e}";
MatchCollection matches = Regex.Matches(text, regex, RegexOptions.Multiline);
Console.WriteLine(matches.Count);
我们可能想尝试的一种方法是测试我们的表达式是否适用于另一种语言。
此外,我们可能想简化表达式:
^(.*?)([\s:]+)?{([\s\S].*)?.$
我们有三个捕获组。第一个和第三个是我们想要的键和值。
正则表达式
您可以 modify/simplify/change 在 regex101.com 中表达您的表情。
正则表达式电路
您还可以在 jex.im:
中可视化您的表情JavaScript 演示
const regex = /^(.*?)([\s:]+)?{([\s\S].*)?.$/gm;
const str = `key:{value}
key:{valu{0}e}
key:{valu
{0}e}
key: {val-u{0}e}
key: {val__[!]
-u{0}{1}e}`;
const subst = `,`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
C# 测试
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"^(.*?)([\s:]+)?{([\s\S].*)?.$";
string substitution = @",";
string input = @"key:{value}
key:{valu{0}e}
key:{valu
{0}e}
key: {val-u{0}e}
key: {val__[!]
-u{0}{1}e}";
RegexOptions options = RegexOptions.Multiline;
Regex regex = new Regex(pattern, options);
string result = regex.Replace(input, substitution);
}
}
原始正则表达式测试
const regex = /^(?<key>\w+?)\s*?:\s*?{(?<value>[\s\S]+?)}$/gm;
const str = `key:{value}
key:{valu{0}e}
key:{valu
{0}e}
key: {val-u{0}e}
key: {val__[!]
-u{0}{1}e}`;
const subst = `,`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
参考文献:
- String.replaceAll without RegEx
- C# regular expression match
- Regular Expression Groups in C#
- c# regex matches example
- Regex: How to get a group name
- How to use inline modifiers in C# regex?
- Regex to first match, then replace found matches