简体   繁体   English

无法从字符串中提取双数

[英]Unable to extract double number from string

I need to extract a decimal number from string in string format. 我需要以字符串格式从字符串中提取一个十进制数字。 This is my approach: 这是我的方法:

 koordinat1 = koordinatarray[0]; //"ENLEM: 39.91666666666670 "
 koordinat2 = koordinatarray[1]; //"\n44.03166666666670"

 Regex regex = new Regex(@"^-?\d+(?:\.\d+)?");
 Match match = regex.Match(koordinat1);

 if (match.Success) {
     koordinat11 = match.Value;
 }

 Match match2 = regex.Match(koordinat2);

 if (match2.Success) {
     koordinat12 = match2.Value;
 }

When I use this code, both koordinat11 and koordinat12 comes empty strings. 当我使用此代码时, koordinat11koordinat12空字符串。 Why can't I get koordinat11 and koordinat12 correctly? 为什么我koordinat12正确获取koordinat11koordinat12

How about Regex regex = new Regex(@"-?\\d+(\\.\\d+)?"); Regex regex = new Regex(@"-?\\d+(\\.\\d+)?"); ?

EDIT: "^-?\\d+(?:\\.\\d+)?" 编辑: "^-?\\d+(?:\\.\\d+)?" doesn't work because ^ symbol states that the string you're trying to find matches in should begin with a match meaning it would need to have a decimal number at the beginning (and in your case the first string begins with a text and second with a new line character). 不起作用,因为^符号指出您要在其中查找匹配项的字符串应以匹配项开头,这意味着它的开头必须为十进制数字(在您的情况下,第一个字符串以文本开头,第二个字符串带有换行符)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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