Regex to validate a number with only one digit following a decimal - regex

I have a regex which validates the user in entering a number with only one digit after a decimal.
However, if I enter: 0.1, 0.2, 0.3, ..., 0.9, it says invalid for each. How do I correct this regex to also accept the above values?
/^[1-9][0-9]{0,3}$|^[1-9][0-9]{0,3}[\.][0-9]$/

You're requiring the first digit to be 1-9.

I think what you want is even simpler than what you have there. Your regex is failing because of the [1-9] at the start since it is forcing the answer to have a 1-9 at the beginning so...
/^[0-9][0-9]{0,3}$|^[1-9][0-9]{0,3}[\.][0-9]$/
I might be mistaken but I think you could do all of this with a much more simplified regex...
/^\d+(?:\.\d)?$/

/^[0-9]{0,4}$|^[0-9]{0,4}[\.][0-9]$/
Or more succinctly
/^\d{0,4}$|^\d{0,4}[\.]\d$/
You were requiring [1-9] prior to the decimal.

Since your regex is structured to disallow leading zeros in the existing cases, you probably want to permit only one zero before the decimal point when the integer portion is zero. That means adding another alternative:
/^(?:[1-9][0-9]{0,3}$|[1-9][0-9]{0,3}\.[0-9]|0\.[0-9])$/
But what about 0 alone? Is it valid input? If so, you can make the fraction part optional in that alternative:
/^(?:[1-9][0-9]{0,3}$|[1-9][0-9]{0,3}\.[0-9]|0(?:\.[0-9])?)$/
That also allows 0.0. If you don't want that, you can change the final [0-9] to [1-9]:
/^(?:[1-9][0-9]{0,3}$|[1-9][0-9]{0,3}\.[0-9]|0(?:\.[1-9])?)$/
Also, notice how I enclosed the alternation in a group, so I only have to use one ^ and one $ for the whole regex.

Related

Regex with constraints, look-ahead function

I'm trying to write a regex expression between 10 and 12 digits. There will be optional leading 0 (zeros) between {0,5} then a numeric string between 10-12 digits. Regardless of the number of zeros (0 to 5), I want 10-12 digits after leading zeros
Example:
0000012345 should not be passing
0012345678 should not be passing as there are only 8 digits after leading zeros
I've tried:
^(0{0,5}(?=\d{10,12}$)^\d{1,2}?\s?(\d{10})$
I think
^0{0,5}[1-9]\d{9,11}$
should be what you need. It enforces not counting the leading zeroes as one of the later digits by requiring it be non-zero. Then there can be 9-11 other digits (including 0).
If you need to include an optional space at any point (as suggested by your RegEx), the RegEx would grow a lot, and it might be easier to do this with some additional code. However, if you give the exact requirements, I will edit the answer accordingly.
^0{0,5}+\d{0,2}\s?\d{10}$
^^
You didn't specify the language. You need "possessive quantifier" here.
See demo:
https://regex101.com/r/m5sOAJ/1
Or if your regex does not support possessive quantifiers:
^(?=(0{0,5}))\1\d{0,2}\s?\d{10}$
See demo:
https://regex101.com/r/m5sOAJ/3

how to find integer with comma and zeros after that (regex)?

I try to create regex(es) to extract all integers. It can be 6 -12 bur also +6.000 or -5,0 and onother one to extract real numbers which are not integers, for example 3.14, -6,26 but no 5.0.
For finding integers I tried "^[+-]?([0-9]+)(\\[.,]0{1,})?$" but it doesn't work on -6.00. And I have no idea how to create second regex (how to exclude integers with comas or dots and then zeros). Any help appreciated.
The problem with your integer regex appears to be the backslash(es). I don't know any regex engine in which you would need to escape the opening bracket of a character class, and you certainly don't want to match a literal backslash. Also, to a regex engine that understands it at all, the quantifier {1,} is an uglier, more complex way of saying +.
This should do your integer matching:
"^[+-]?[0-9]+([.,]0+)?$"
And this variation should do your non-integer matching:
"^[+-]?[0-9]+[.,]0*[1-9][0-9]*$"
In both cases I omitted parentheses not needed for expressing a correct pattern, but if you need to capture parts of the match then you will want to add some back in. You might also want to convert the grouping parentheses into non-capturing form if you are using a regex engine that supports it.
Also, the real number pattern requires at least one digit before the fraction separator character, per your examples. It would be easy to convert the pattern to also match strings of the form .1 or -.17. Similarly, the integer pattern requires at least one zero in the fraction part if there is a fraction separator, and restriction could be removed, too.

Invalidate scientific notation with 0 value exponent or trailing 0s

I am using this solution to regex scientific notation:
/-?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?/
I'd like to add a slight amount of space conservation, so I'd like to also forbid trailing zeros after the decimal point and zero value as the exponent.
I think that adding [1-9] after \.\d will enforce no trailing zeros, but I think it will also force at least two numbers after . which is undesirable.
I do not possess the necessary experience to modify this regex properly.
How can my intent be implemented?
You can make this simple change:
/-?(?:0|[1-9]\d*)(?:\.\d*[1-9])?(?:[eE][+-]?[1-9]\d*)?/
Note that [1-9]\d* will forbid exponants with a leading zero.
I think that adding [1-9] after \.\d will enforce no trailing zeros, but I think it will also force at least two numbers after .
No \d is actually \d*, which means that it will only enforce one [1-9] (preceded by none or many \ds). So
/-?(?:0|[1-9]\d*)(?:\.\d*[1-9])?(?:[eE][+\-]?\d+)?/
/-?(?:0|[1-9]\d*)(?:\.\d*[1-9])?(?:[eE][+\-]?[1-9]\d*)?/ # no (leading) zero exponent
will work. It does however enforce one digit after the dot, if a decimal part is apparent.

Regex matching numbers and decimals

I need a regex expression that will match the following:
.5
0.5
1.5
1234
but NOT
0.5.5
absnd (any letter character or space)
I have this that satisfies all but 0.5.5
^[.?\d]+$
This is a fairly common task. The simplest way I know of to deal with it is this:
^[+-]?(\d*\.)?\d+$
There are also other complications, such as whether you want to allow leading zeroes or commas or things like that. This can be as complicated as you want it to be. For example, if you want to allow the 1,234,567.89 format, you can go with this:
^[+-]?(\d*|\d{1,3}(,\d{3})*)(\.\d+)?\b$
That \b there is a word break, but I'm using it as a sneaky way to require at least one numeral at the end of the string. This way, an empty string or a single + won't match.
However, be advised that regexes are not the ideal way to parse numeric strings. All modern programming languages I know of have fast, simple, built-in methods for doing that.
Here's a much simpler solution that doesn't use any look-aheads or look-behinds:
^\d*\.?\d+$
To clearly understand why this works, read it from right to left:
At least one digit is required at the end.
7 works
77 works
.77 works
0.77 works
0. doesn't work
empty string doesn't work
A single period preceding the digit is optional.
.77 works
77 works
..77 doesn't work
Any number of digits preceding the (optional) period.
.77 works
0.77 works
0077.77 works
0077 works
Not using look-aheads and look-behinds has the added benefit of not having to worry about RegEx-based DOS attacks.
HTH
Nobody seems to be accounting for negative numbers. Also, some are creating a capture group which is unnecessary. This is the most thorough solution IMO.
^[+-]?(?:\d*\.)?\d+$
The following should work:
^(?!.*\..*\.)[.\d]+$
This uses a negative lookahead to make sure that there are fewer than two . characters in the string.
http://www.rubular.com/r/N3jl1ifJDX
This could work:
^(?:\d*\.)?\d+$

How can I check if at least one of two subexpressions in a regular expression match?

I am trying to match floating-point decimal numbers with a regular expression. There may or may not be a number before the decimal, and the decimal may or may not be present, and if it is present it may or may not have digits after it. (For this application, a leading +/- or a trailing "E123" is not allowed). I have written this regex:
/^([\d]*)(\.([\d]*))?$/
Which correctly matches the following:
1
1.
1.23
.23
However, this also matches empty string or a string of just a decimal point, which I do not want.
Currently I am checking after running the regex that $1 or $3 has length greater than 0. If not, it is not valid. Is there a way I can do this directly in the regex?
I think this will do what you want. It either starts with a digit, in which case the decimal point and digits after it are optional, or it starts with a decimal point, in which case at least one digit is mandatory after it.
/^\d+(\.\d*)?|\.\d+$/
Create a regular expression for each case and OR them. Then you only need test if the expression matches.
/^(\d+(\.\d*)?)|(\d*\.\d+)$/
A very late answer, but like to answer, taken from regular-expressions.info
[-+]?[\d]*\.?[\d]+?
Update This [\d]*\.?[\d]+?|[\d]+\. will help you matching 1.
http://regex101.com/r/lJ7fF4/7