0

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]+).");
1
  • Are these examples full strings and you want to validate their correct format? Or do you want to find substrings like these in larger input strings? Commented Dec 4, 2012 at 16:28

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

@Some1.Kill.The.DJ.. Why? It is required to match the following string.
that would also match 435.5px hello world if you donot use groups..OP wants output like 100px 100 px 1.0ft
@Some1.Kill.The.DJ.. But this is nowhere specified in the question that he wants to match words. OP said he wants to match sentence. But this can be altered easily to match words.
1

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();      

Comments

0

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

1 Comment

string mysubject="..."; If (char.IsDigit(mysubject.FirstOrDefault()) ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.