如何使用Regex捕获此值? [重复]

时间:2022-12-01 09:57:06

This question already has an answer here:

这个问题在这里已有答案:

I have this text: "Showing: 16 of 11543 course results"

我有这样的文字:“显示:11543课程结果中的16个”

I want to capture 16 and 11543. This value could differ but their position will always be the same. I could split this text but it doesn't look nice, and can lead to bugs in future. Is there any way I could achieve this using Regex?

我想捕获16和11543.这个值可能不同,但它们的位置将始终相同。我可以分割这个文本,但它看起来不太好,并可能在将来导致错误。有什么办法可以用Regex实现这个目的吗?

2 个解决方案

#1


1  

Here is the regex:

这是正则表达式:

Showing: (\d{1,10}) of (\d{1,10}) course results

Capture group is your first number, capture group 2 is your second number. Capture groups are defined by the brackets () , the \d is capturing digits, and the number in the curly braces defines the possible length of the digits to be captured.

捕获组是您的第一个数字,捕获组2是您的第二个数字。捕获组由方括号()定义,\ d是捕获数字,大括号中的数字定义要捕获的数字的可能长度。

So in this case it will detect number that consists at least of 1 and maximum of 10 digits.

因此,在这种情况下,它将检测至少包含1和最多10位数的数字。

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string yourInputString = "Showing: 16 of 11543 course results";
        Match match = Regex.Match(yourInputString, @"Showing: (\d{1,10}) of (\d{1,10}) course results", RegexOptions.IgnoreCase);
        if (match.Success)
        {
            string firstNum = match.Groups[1].Value; // 16
            string secondNum = match.Groups[2].Value; // 11543
        }
    }
}

#2


-1  

There are so many ways to resolve this... another one that finds all digit-groups:

有很多方法可以解决这个问题...另一个找到所有数字组的方法:

string text = "Showing: 16 of 11543 course results";
Regex re = new Regex("(\\d+)");
MatchCollection mc = re.Matches(text);
foreach(var match in mc) 
{
  Console.WriteLine(match);
}

#1


1  

Here is the regex:

这是正则表达式:

Showing: (\d{1,10}) of (\d{1,10}) course results

Capture group is your first number, capture group 2 is your second number. Capture groups are defined by the brackets () , the \d is capturing digits, and the number in the curly braces defines the possible length of the digits to be captured.

捕获组是您的第一个数字,捕获组2是您的第二个数字。捕获组由方括号()定义,\ d是捕获数字,大括号中的数字定义要捕获的数字的可能长度。

So in this case it will detect number that consists at least of 1 and maximum of 10 digits.

因此,在这种情况下,它将检测至少包含1和最多10位数的数字。

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string yourInputString = "Showing: 16 of 11543 course results";
        Match match = Regex.Match(yourInputString, @"Showing: (\d{1,10}) of (\d{1,10}) course results", RegexOptions.IgnoreCase);
        if (match.Success)
        {
            string firstNum = match.Groups[1].Value; // 16
            string secondNum = match.Groups[2].Value; // 11543
        }
    }
}

#2


-1  

There are so many ways to resolve this... another one that finds all digit-groups:

有很多方法可以解决这个问题...另一个找到所有数字组的方法:

string text = "Showing: 16 of 11543 course results";
Regex re = new Regex("(\\d+)");
MatchCollection mc = re.Matches(text);
foreach(var match in mc) 
{
  Console.WriteLine(match);
}