简体   繁体   中英

Using regex to extract multiple numbers from strings

I have a string with two or more numbers. Here are a few examples:

"(1920x1080)"
" 1920 by 1080"
"16 : 9"

How can I extract separate numbers like "1920" and "1080" from it, assuming they will just be separated by one or more non-numeric character(s)?

The basic regular expression would be:

[0-9]+

You will need to use the library to go over all matches and get their values.

var matches = Regex.Matches(myString, "[0-9]+");

foreach(var march in matches)
{
   // match.Value will contain one of the matches
}

You can get the string by following

MatchCollection v = Regex.Matches(input, "[0-9]+");
foreach (Match s in v)
            {
                // output is s.Value
            }
(\d+)\D+(\d+)

之后,自定义此正则表达式以匹配您将使用的语言的风格。

you can use

string[] input = {"(1920x1080)"," 1920 by 1080","16 : 9"};
foreach (var item in input)
{
    var numbers = Regex.Split(item, @"\D+").Where(s => s != String.Empty).ToArray();
    Console.WriteLine("{0},{1}", numbers[0], numbers[1]);
}

OUTPUT:

1920,1080
1920,1080
16,9

There is still an issue though, all above answers consider 12i or a2 valid numbers when they shouldn't.

The following could solve this issue

var matches = Regex.Matches(input, @"(?:^|\s)\d+(?:\s|$)");

But this solution adds one more issue :) This will capture the spaces around the integer. To solve this we need to capture the value of the integer into a group:

MatchCollection matches = Regex.Matches(_originalText, @"(?:^|\s)(\d+)(?:\s|$)");
HashSet<string> uniqueNumbers = new HashSet<string>();
foreach (Match m in matches)
{
    uniqueNumbers.Add(m.Groups[1].Value);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM