0

How to find number(int, float) in a string (fast method)?
For example: "Question 1" and "Question 2.1". I need that in variable would be only number.
Thanks.

user348173
  • 8,818
  • 18
  • 66
  • 102
  • One of your questions is about regular expressions. theres your answer – skyfoot Jul 27 '11 at 09:50
  • possible duplicate of [c# find and extract number from a string](http://stackoverflow.com/questions/4734116/c-find-and-extract-number-from-a-string) –  Jul 27 '11 at 09:53

4 Answers4

3

you can use this ([0-9]\.*)*[0-9] regex to get it. you can test any regex here

Edit

this sample code in C#

Regex regexpattern = new Regex(@"(([0-9]\.*)*[0-9])");
String test = @"Question 1 and Question 2.1.3";
foreach (Match match in regexpattern.Matches(test))
{
    String language = match.Groups[1].Value;
}
Amir Ismail
  • 3,865
  • 3
  • 20
  • 33
1

You can use Reqular Expressions. To search numbers try this:

var r = System.Text.RegularExpressions.Regex.Match("Question 2.1", "\d+");
Sergey Shulik
  • 950
  • 1
  • 9
  • 24
1

There's always the good ol' regular expressions! It wouldn't give you a float for "2.1" though, but I'm not sure that's a good idea since there's always the possibility of "2.1.4" or even "2a". Might be best to store a vector of numbers for each item.

Skizz
  • 69,698
  • 10
  • 71
  • 108
1

Use this regex: \d+(?:\.\d+)?.

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125