如何在C#Regex中匹配“参数名称:值”?

时间:2021-07-06 10:35:26

I would like to match these lines:

我想匹配这些线:

ParameterINeed: 758
ParameterCount: 8695
ParameterText: 56

And I would receive a parameter name and parameter value. Could you please tell me how to write Regex.Matches patter for this and how to process this data into Dictionary?

我会收到一个参数名称和参数值。你能告诉我如何为此编写Regex.Matches模式以及如何将这些数据处理成字典吗?

I use this code:

我用这个代码:

string Text = "ParameterINeed: 758\r\nParameterCount: 8695\r\nParameterText: 56";
string Pattern = "^(\\w+):\\s+(\\d+)$";
MatchCollection ma = Regex.Matches(Text, Pattern, RegexOptions.Singleline);

And get ma.Count = 0

得到ma.Count = 0

6 个解决方案

#1


The RegexOptions.SingleLine only affects how the period token works, not how the ^ and $ tokens work. You need to use RegexOptions.MultiLine for that.

RegexOptions.SingleLine仅影响句点标记的工作方式,而不影响^和$标记的工作方式。您需要使用RegexOptions.MultiLine。

The multiline mode doesn't understand the \r\n line breaks, it only considers the \n character as line break. You have to consume the \r character to get to the line break.

多行模式不理解\ r \ n换行符,它只将\ n字符视为换行符。您必须使用\ r \ n字符才能进入换行符。

string text = "ParameterINeed: 758\r\nParameterCount: 8695\r\nParameterText: 56";
string pattern = @"^(\w+):\s+(\d+)\r?$";
MatchCollection ma = Regex.Matches(text, pattern, RegexOptions.Multiline);

Now ma.Count is 3.

现在ma.Count是3。

This is how you put the matches in a dictionary:

这是你将匹配放在字典中的方式:

Dictionary<string, int> values = new Dictionary<string, int>();
foreach (Match match in ma) {
    values.Add(match.Groups[1].Value, int.Parse(match.Groups[2].Value));
}

#2


Try this regex

试试这个正则表达式

"^Parameter(\w+):\s+(\d+)$"

You can then acces the name via Matches[1] and the value as Matches[2]. My answer is based on the idea that for the string ParameterINeed: 42 you want

然后,您可以通过匹配[1]访问名称,将值作为匹配[2]。我的回答是基于你想要的字符串ParameterINeed:42的想法

  • Name: INeed
  • Value: 42

If instead you wanted ParameterINeed for the value, you could just remove the Parameter word from the regex.

如果您想要使用ParameterINeed作为值,则可以从正则表达式中删除参数字。

"^(\w+):\s+(\d+)$"

EDIT Responding to added code sample

编辑响应添加的代码示例

Try the following sample instead

请尝试以下示例

string Text = "ParameterINeed: 758\r\nParameterCount: 8695\r\nParameterText: 56";
string[] lines = Text.Split("\n");
string Pattern = @"^(\w+):\s+(\\d+)$";
foreach ( string line in lines ) {
  MatchCollection ma = Regex.Matches(line, Pattern, RegexOptions.Singleline);
}

#3


Why don't you simply split by lines, then the text by ':' and trim the results? Or is it more complex issue?

你为什么不简单地按行分割,然后用':'分割文本并修剪结果?还是更复杂的问题?

#4


Not having used C# I can't give a direct code sample.

没有使用C#我不能给出直接的代码示例。

If it follows normal regex patterns though, the problem might be the ^/$.. normally that matches start of string (^) and end of string ($) not necessarily "end of line".

如果它遵循正常的正则表达式模式,问题可能是^ / $ ..通常匹配字符串(^)的开头和字符串结尾($)不一定是“行尾”。

What if you try something like (tested with perl):

如果你尝试类似的东西(用perl测试)怎么办:

/(\w+):\s(\w+)(?:\r\n)?/g

#5


Here is a tested solution. ;)

这是经过测试的解决方案。 ;)

static void Main(string[] args)
{
    try
    {

        string sInput;

        // The string to search.
        sInput = "ParameterINeed: 758\r\nParameterCount: 8695\r\nParameterText: 56";


        var regex = new Regex(@"(?<parameter>\w+):\s+(?<value>\d+)");
        var dictionary = new Dictionary<string, string>();

        foreach (Match match in regex.Matches(sInput))
        {
            dictionary.Add(
            match.Groups["parameter"].Value, 
            match.Groups["value"].Value); 
        }

        foreach (KeyValuePair<string, string> item in dictionary)
            Console.WriteLine("key: {0}; value:{1}", item.Key, item.Value);

    }
    finally
    {
        Console.ReadKey();
    }

}

#6


Depends on some other constaints but you could just use this.

取决于其他一些constaints但你可以使用它。

var regex = new Regex(@"^($<parameter>\w+):\s($<value>\d+)$");
var dictionary = new Dictionary<string, string>();

foreach(var match in regex.Matches(inputData, RegexOptions.Multiline))
{
  dictionary.Add(
    match.Groups["parameter"].Value, 
    match.Groups["value"].Value);
}

#1


The RegexOptions.SingleLine only affects how the period token works, not how the ^ and $ tokens work. You need to use RegexOptions.MultiLine for that.

RegexOptions.SingleLine仅影响句点标记的工作方式,而不影响^和$标记的工作方式。您需要使用RegexOptions.MultiLine。

The multiline mode doesn't understand the \r\n line breaks, it only considers the \n character as line break. You have to consume the \r character to get to the line break.

多行模式不理解\ r \ n换行符,它只将\ n字符视为换行符。您必须使用\ r \ n字符才能进入换行符。

string text = "ParameterINeed: 758\r\nParameterCount: 8695\r\nParameterText: 56";
string pattern = @"^(\w+):\s+(\d+)\r?$";
MatchCollection ma = Regex.Matches(text, pattern, RegexOptions.Multiline);

Now ma.Count is 3.

现在ma.Count是3。

This is how you put the matches in a dictionary:

这是你将匹配放在字典中的方式:

Dictionary<string, int> values = new Dictionary<string, int>();
foreach (Match match in ma) {
    values.Add(match.Groups[1].Value, int.Parse(match.Groups[2].Value));
}

#2


Try this regex

试试这个正则表达式

"^Parameter(\w+):\s+(\d+)$"

You can then acces the name via Matches[1] and the value as Matches[2]. My answer is based on the idea that for the string ParameterINeed: 42 you want

然后,您可以通过匹配[1]访问名称,将值作为匹配[2]。我的回答是基于你想要的字符串ParameterINeed:42的想法

  • Name: INeed
  • Value: 42

If instead you wanted ParameterINeed for the value, you could just remove the Parameter word from the regex.

如果您想要使用ParameterINeed作为值,则可以从正则表达式中删除参数字。

"^(\w+):\s+(\d+)$"

EDIT Responding to added code sample

编辑响应添加的代码示例

Try the following sample instead

请尝试以下示例

string Text = "ParameterINeed: 758\r\nParameterCount: 8695\r\nParameterText: 56";
string[] lines = Text.Split("\n");
string Pattern = @"^(\w+):\s+(\\d+)$";
foreach ( string line in lines ) {
  MatchCollection ma = Regex.Matches(line, Pattern, RegexOptions.Singleline);
}

#3


Why don't you simply split by lines, then the text by ':' and trim the results? Or is it more complex issue?

你为什么不简单地按行分割,然后用':'分割文本并修剪结果?还是更复杂的问题?

#4


Not having used C# I can't give a direct code sample.

没有使用C#我不能给出直接的代码示例。

If it follows normal regex patterns though, the problem might be the ^/$.. normally that matches start of string (^) and end of string ($) not necessarily "end of line".

如果它遵循正常的正则表达式模式,问题可能是^ / $ ..通常匹配字符串(^)的开头和字符串结尾($)不一定是“行尾”。

What if you try something like (tested with perl):

如果你尝试类似的东西(用perl测试)怎么办:

/(\w+):\s(\w+)(?:\r\n)?/g

#5


Here is a tested solution. ;)

这是经过测试的解决方案。 ;)

static void Main(string[] args)
{
    try
    {

        string sInput;

        // The string to search.
        sInput = "ParameterINeed: 758\r\nParameterCount: 8695\r\nParameterText: 56";


        var regex = new Regex(@"(?<parameter>\w+):\s+(?<value>\d+)");
        var dictionary = new Dictionary<string, string>();

        foreach (Match match in regex.Matches(sInput))
        {
            dictionary.Add(
            match.Groups["parameter"].Value, 
            match.Groups["value"].Value); 
        }

        foreach (KeyValuePair<string, string> item in dictionary)
            Console.WriteLine("key: {0}; value:{1}", item.Key, item.Value);

    }
    finally
    {
        Console.ReadKey();
    }

}

#6


Depends on some other constaints but you could just use this.

取决于其他一些constaints但你可以使用它。

var regex = new Regex(@"^($<parameter>\w+):\s($<value>\d+)$");
var dictionary = new Dictionary<string, string>();

foreach(var match in regex.Matches(inputData, RegexOptions.Multiline))
{
  dictionary.Add(
    match.Groups["parameter"].Value, 
    match.Groups["value"].Value);
}