使用正则表达式从字符串中提取多个数字 [英] 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"
如何提取分开的数字,例如 1920和
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)?
推荐答案
基本正则表达式为:
[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
}
这篇关于使用正则表达式从字符串中提取多个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文