locked
how to insert only numbers in Regex RRS feed

  • Question

  • hi

    how to insert only numbers in Regex ?

    for example:

    good:  123454

    no goog: 123fg456

    no good: 3..445-=43

    thanks,

    • Moved by Lisa Zhu Wednesday, June 20, 2012 9:13 AM Regex question (From:Visual C# Language)
    Tuesday, June 19, 2012 8:44 AM

Answers

  • try

    ^(0|[1-9][0-9]*)$


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Tuesday, June 19, 2012 8:54 AM
  • or:

    @"[^0-9\.]+"


    Mitja

    Tuesday, June 19, 2012 2:16 PM
  • or:

    @"[^0-9\.]+"

    If . is allowed, you can use @"^[\d.]+$" or "^[0-9.]+$"

    Don't forget the ^ and $ or you're going to match anything containing a digit.

    If 1.5 is allowed but not 1.2.3, you can use something like @"^\d+(\.[\d+])?$"

    Tuesday, June 19, 2012 3:08 PM

All replies

  • try

    ^(0|[1-9][0-9]*)$


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Tuesday, June 19, 2012 8:54 AM
  • he Regular Expression forums http://social.msdn.microsoft.com/Forums/en-US/regexp/threads is the better place for such questions

    With Thanks and Regards
    Sambath Raj.C
    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
    Happy Programming!

    Tuesday, June 19, 2012 10:02 AM
  • thanks for the help,

    but if i insert 0012 -> it need to be good but its not, why ?

    Tuesday, June 19, 2012 10:05 AM
  • hi

    how to insert only numbers in Regex ?

    for example:

    good:  123454

    good: 00123

    no goog: 123fg456

    no good: 3..445-=43

    thanks,

    Tuesday, June 19, 2012 10:15 AM
  • //C#.Net

    Regex regex = new Regex("^[0-9]*$");

    regex.IsMatch(textBox1.Text))


    With Thanks and Regards
    Sambath Raj.C
    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
    Happy Programming!

    Tuesday, June 19, 2012 10:45 AM
  • Because the regex explicitly excludes numbers starting with 0.

    Try "^\d+$" instead

    Tuesday, June 19, 2012 2:09 PM
  • or:

    @"[^0-9\.]+"


    Mitja

    Tuesday, June 19, 2012 2:16 PM
  • or:

    @"[^0-9\.]+"

    If . is allowed, you can use @"^[\d.]+$" or "^[0-9.]+$"

    Don't forget the ^ and $ or you're going to match anything containing a digit.

    If 1.5 is allowed but not 1.2.3, you can use something like @"^\d+(\.[\d+])?$"

    Tuesday, June 19, 2012 3:08 PM
  • So You just want to match numbers. If so,try this

    ^\d+$ 

    note this only match numbers.

    Match only those has Yellow background

    ----------------------------------------

    1123
    test
    123.11
    wtwwt7878
    qweqwe
    12.3333

    Tuesday, June 26, 2012 6:45 AM
  • You can use this Regex Expression

    ^\d{5}$

    this expression Matches 5 numeric digits, such as a zip code.

    I am shairing a ver great site for various Regular Expressions collection and tester also.

    http://www.regexlib.com/Search.aspx?k=numeric

    Kindly check it once....

    Thanks & Regards

    Gaurav Agrawal

    http://planetofcoders.com/


    Friday, June 29, 2012 10:23 AM
  • I don't think it would be feasible to extract only digits from the above data format considering there is no such specific pattern of data set.

    Hope this RE helps in getting the approach towards the near solution. This RE considers that the value on left hand side are columns and the values on the right side after colon are the data that need to be extracted.

    ^\w+\s*:\s*([\d]+).*?(?:[\w]+\s+[\w]+)\s*:\s*([\d\w]+).*?(?:[\w]+\s+[\w]+)\s*:\s*([-=\.\d\w]+)

    Hope this helps.

    Regards,


    Ankur Akash

    Wednesday, July 18, 2012 2:55 PM