Dmytro
Dmytro

Reputation: 17166

Need regex to match strings which begin with numbers

I need regular expression to match strings, which begin from number (number can be integer or float). For example:

100px
100 px
1.0ft
1.0 ft
0.001ft2
0.001 ft2

I'm new in this stuff, can anyone help me, please? I've already tried something like:

Regex numberBeginRegex = new Regex(@"([\d]+|[\d]+[.][\d]+).");

Upvotes: 0

Views: 215

Answers (3)

Anirudha
Anirudha

Reputation: 32787

You can use this regex

 var reg=@"^(\d+(\.\d+)?).*";
 List<string> nums=Regex.Matches(inp,reg,RegexOptions.Multiline)
                        .Cast<Match>()
                        .Select(x=>x.Value)
                        .ToList();      

Upvotes: 1

Rohit Jain
Rohit Jain

Reputation: 213203

You can use this regex: -

"(\d+(\.\d+)?).*"

(\d+(\.\d+)?) - matches integer number or floating point numbers. The fractional part is made optional by using ? quantifier, which means - match 0 or 1


Actually your regex would have worked too, but you forgot to put * quantifier at the end of .: -

"([\d]+|[\d]+[.][\d]+).*"  // Note the `*` at the end

Upvotes: 3

CR41G14
CR41G14

Reputation: 5594

If you want to check if they start with a number you can use substring() to get the first char

Upvotes: 0

Related Questions