jason
jason

Reputation: 7174

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. Why can't I get koordinat11 and koordinat12 correctly?

Upvotes: 1

Views: 78

Answers (1)

suwik
suwik

Reputation: 110

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

EDIT: "^-?\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).

Upvotes: 2

Related Questions