Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Regular expression to accept only comma, dot & space between numbers

Last post 01-12-2009, 7:47 PM by Aussie Susan. 4 replies.
Sort Posts: Previous Next
  •  01-07-2009, 12:51 AM 49929

    Regular expression to accept only comma, dot & space between numbers

    Hi all,

    I have a requirement where the text box should accept only comma, dot & space between numbers.
    ex: 123
        123,456.00 (UK)   
        123 456,00 (French)

    it should not accept two consecutive commas, dots and space(123,,/12..3)

    Please can any one help me on this

     

    Thanks in advance 

     

     



  •  01-07-2009, 10:48 AM 49936 in reply to 49929

    Re: Regular expression to accept only comma, dot & space between numbers

    Hi,

    i would take this: \d+(?:,|\s|\.)?\d*(?:,|\s|\.)?\d{0,2}

  •  01-12-2009, 5:06 AM 50061 in reply to 49936

    Re: Regular expression to accept only comma, dot & space between numbers

    Hi

     Thanks for your response.

     Reg Expression specified above doesn't solve the purpose.it should not accept two consecutive commas, dots and space

    ex: 123,,4.00 / 12..34.00/123,45,,0/123  03.00

     i tried with some examples like 1,-2,33.00 , its accepts.

    can you please help me on this.

     

     

  •  01-12-2009, 1:01 PM 50071 in reply to 50061

    Re: Regular expression to accept only comma, dot & space between numbers

    You want to match nothing on the samples above?

    Well the only idea i had was this: ^(?!\d+?(,|\s|\.){2,})\d+(?:,|\s|\.)?\d*(?:,|\s|\.)?\d{0,2}

    The following don't match at all:

     123,,4.00

     12..34.00

    123  03.00

    The following stops matching after it gets wrong:

     123,45,,0 (stops when the second "," comes)

     1,-2,33.00 (stops when "-" comes)

    12.765.. (stops after the second dot)

    The following matches complete:

     123,456.00

    123 456,00

     

     

  •  01-12-2009, 7:47 PM 50079 in reply to 49929

    Re: Regular expression to accept only comma, dot & space between numbers

    Without knowing the version of regex engine you are working with, the normal way I solve this type of problem is:

    ^(?!.*?([,.\x20])\1)[\d,.\x20]+$

    The '^' and '$' are there to make sure that we deal with everything in the text (assuming the multiline is off).

    The negative lookahead skips over as many characters as needed (including none) until it finds a comma, period (dot or full-stop) or space character. If it does, it checks to see if the next character is the same and (because it is a negative lookahead) will reject the match if tis is so. (Note - this works in this case because you are looking for consecutive characters. If your requirement is that there is only a single decimal character and not necessarily consecutive, then a slight variation would be needed).

    If all that checks out, then we know there are no consecutive comma, period or space characters so we can then check that the characters are all digits, spaces, commas or periods. This also makes sure that the line is not empty be requiring at least one of these characters (although it will accept a line that is a single space or comma etc.). However, given your requirements, this should do the trick.

    Susan 

View as RSS news feed in XML