Reputation: 17166
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
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
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
Reputation: 5594
If you want to check if they start with a number you can use substring()
to get the first char
Upvotes: 0