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)?
我如何从中提取出像“1920”和“1080”这样的独立数字,假设它们将被一个或多个非数字字符分隔?
5 个解决方案
#1
9
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
}
#2
5
You can get the string by following
您可以通过以下方式获取该字符串。
MatchCollection v = Regex.Matches(input, "[0-9]+");
foreach (Match s in v)
{
// output is s.Value
}
#3
1
(\d+)\D+(\d+)
After that, customize this regex to match the flavour of the language you'll be using.
然后,定制这个regex以匹配您将要使用的语言的风格。
#4
1
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
#5
0
There is still an issue though, all above answers consider 12i or a2 valid numbers when they shouldn't.
但是仍然有一个问题,上面所有的答案都考虑12i或a2的有效数字,当它们不应该考虑的时候。
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);
}
#1
9
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
}
#2
5
You can get the string by following
您可以通过以下方式获取该字符串。
MatchCollection v = Regex.Matches(input, "[0-9]+");
foreach (Match s in v)
{
// output is s.Value
}
#3
1
(\d+)\D+(\d+)
After that, customize this regex to match the flavour of the language you'll be using.
然后,定制这个regex以匹配您将要使用的语言的风格。
#4
1
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
#5
0
There is still an issue though, all above answers consider 12i or a2 valid numbers when they shouldn't.
但是仍然有一个问题,上面所有的答案都考虑12i或a2的有效数字,当它们不应该考虑的时候。
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);
}