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
Re: Some advice on regular expressions
try:
"^[[0-9]+(\.\,)[0-9]+]$"
Re: Some advice on regular expressions
Quote:
Originally Posted by
.paul.
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
Re: Some advice on regular expressions
^\d{1,3}(?:\,?\d{3})*(?:\.\d+){0,1}
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.
Re: Some advice on regular expressions
Quote:
Originally Posted by
Sitten Spynne
..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.
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
Re: Some advice on regular expressions
Try this:
Code:
"^(\d+(\.|\,)\d+)$"
Re: Some advice on regular expressions
The important part is: (\.|\,)
it means choose 1 or the other
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.
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.
Re: Some advice on regular expressions
Quote:
Originally Posted by
Sitten Spynne
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
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).
Re: Some advice on regular expressions
Quote:
Originally Posted by
.paul.
??? 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