Results 1 to 14 of 14

Thread: Some advice on regular expressions

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Some advice on regular expressions

    Although not directly related to vb.net, I couldn't find a better home for this question.

    I am doing on my form some input validation, and trying to come up with a regular expression that will allow me to input positive decimals with both , and . as delimiter, no matter which is the current locale. Everything is working "almost perfectly.

    This is the reg exp I am using: ^[0-9.,\-]+$

    It works okay with one problem. It allows me to input multiple dots and commas. For example 2,.,.,.,.4 it allows.

    How can I write this regexp so it only allow ONE , or . after the first number?


    best regards
    H

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Some advice on regular expressions

    try:

    "^[[0-9]+(\.\,)[0-9]+]$"

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Re: Some advice on regular expressions

    Quote Originally Posted by .paul. View Post
    try:

    "^[[0-9]+(\.\,)[0-9]+]$"
    Hi!

    I have no luck, either in VB.NET nor on https://regex101.com/

    no characters are accepted by that reg exp...

    /H

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Some advice on regular expressions

    ^\d{1,3}(?:\,?\d{3})*(?:\.\d+){0,1}
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Some advice on regular expressions

    By "delimiter" do you mean you want grouping, like "1,000,000" or simply that you want to allow both commas and decimals to be used as the decimal separator, as in "400,25" and "400.25"?

    .paul., I think you have a [] where you meant (). And dbasnett, you make the assumption that all cultures group digits by 3s (not all do.) Regex is sort of a bad tool for this task, but I want to understand the problem before I try out the solution I'm thinking about.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Some advice on regular expressions

    Quote Originally Posted by Sitten Spynne View Post
    ..And dbasnett, you make the assumption that all cultures group digits by 3s (not all do.) Regex is sort of a bad tool for this task, but I want to understand the problem before I try out the solution I'm thinking about.
    Yes I did assume that he wasn't looking for an answer that was going to be used in India.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Re: Some advice on regular expressions

    I note that my question was not clear.

    My delimiter I don't mean thousand delimiter, but rather a decimal delimiter when trying to input values to a double type.

    I have tested the above reg exps in https://regex101.com/ but I get "no match". Doesn't work better in VB either :/

    The one I posted works well enough, if only I could restrict to ONLY 1 of either , or . between first number and decimals.

    1.3434 allowed
    1,54345 allowed
    1,.3123 NOT ALLOWED

    cheers
    H

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Some advice on regular expressions

    Try this:

    Code:
    "^(\d+(\.|\,)\d+)$"

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Some advice on regular expressions

    The important part is: (\.|\,)
    it means choose 1 or the other

  10. #10
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Some advice on regular expressions

    OK, if all you need is the decimal separator, what .paul. posted will work, though I think it'd more normally be "^\d*[.,]?\d+". That's any number of digits, zero or one of our desired separators, then one or more digits. This is still tricky, maybe you want to accept "45."; in that case you could change the last "+" to a "*".

    If it were me, I'd Double.TryParse() with the current UI culture, but this isn't a heinous deviation from that. Digit grouping is a bit too intense for regex though unless you can narrow it down to very specific cultures.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  11. #11
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,397

    Re: Some advice on regular expressions

    To be honest, I agree with Sitten Spynne... Why are you using RegEx for validation in the first place? If you were to use a NumericUpDown and set the DecimalPlaces then the decimal will conform to the current settings(comma or decimal). Additionally you can set the ThousandsSeparator property to True too.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Some advice on regular expressions

    Quote Originally Posted by Sitten Spynne View Post
    it'd more normally be "^\d*[.,]?\d+".
    ??? at least one of those square bracketed matches needs escaping. Regex definitely won't treat . as a literal

  13. #13
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Some advice on regular expressions

    Are you sure this is going to work for you?

    If both the following are to be allowed
    1.234
    1,234
    and I enter 1,234, then how will you know if I meant a number larger than a thousand or a number less than 2 without knowing if I'm in France or the UK?

    I'd go with what Dave said (NumericUpDown) or what SS said (Double.TryParse) (except it's not not the current thread's CurrentUICulture, it's the current thread's CurrentCulture, which is what is used by the default IFormatProvider for the Parse methods anyways).

  14. #14
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Some advice on regular expressions

    Quote Originally Posted by .paul. View Post
    ??? at least one of those square bracketed matches needs escaping. Regex definitely won't treat . as a literal
    See what I mean about Regex tripping up even experts? Regex is a programming language that describes a parser. Annoyingly, it is a language with several contexts.

    Within square brackets, we are in the "character class description" context. Now we have a completely different set of special characters: "]", "\", and "^". In this context, "." is just a plain old period, and means "include the character '.' in this class".

    Try it!

    Code:
    Dim regex As New Regex("[.,]")
    
    For Each candidate In {"asdf", "a.s", "a,s"}
    	Console.WriteLine("|{0}| -> {1}", candidate, regex.IsMatch(candidate))
    Next
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width