Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从字符串中提取post数值_C#_Asp.net_Linq - Fatal编程技术网

C# 从字符串中提取post数值

C# 从字符串中提取post数值,c#,asp.net,linq,C#,Asp.net,Linq,我想从“KB”(实际数据大小)中获取post数字 比如说 string word1="Product data 5KB per second" string word2="Product data is 5 KB per hour" 如果我通过KB,我想从上面的单词中提取5 var word2Size = Regex.Match(word2, @"\d+(?=\s?KB)").Value; 或用于浮点数 var word2Size = Regex.Match(word2, @"\d+\.?

我想从“KB”(实际数据大小)中获取post数字 比如说

string word1="Product data 5KB per second" 
string word2="Product data is 5 KB per hour"

如果我通过KB,我想从上面的单词中提取5

var word2Size = Regex.Match(word2, @"\d+(?=\s?KB)").Value;
或用于浮点数

var word2Size = Regex.Match(word2, @"\d+\.?\d+(?=\s?KB)").Value;
它应该返回一个数值,紧跟在可选空白之前,后跟KB

可以为不同的后缀生成正则表达式字符串,后缀为:

string regexString = String.Format(@"\d+\.?\d+(?=\s?{0})", mySuffix);
var word2Size = Regex.Match(word2, regexString).Value;

你可以在一个字符串中有很多KB?你试过什么吗?@Amit我的字符串中只有1KB为什么你找不到“Product data is”和“KB”之间的文本?@Amit:第一个示例说“Product data”不是“Product data is”这是一个:D,虽然对于浮点,第二个数字组应该和“.”一样可选。因为+是一个或多个
\d+\。?\d*
谢谢兄弟,很高兴能帮上忙-对于有类似问题的人,你应该将答案标记为正确