Invalidate scientific notation with 0 value exponent or trailing 0s - regex

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.

Related

Definitions in flex lexical analyzer - How to define scientific notation

I'm new to using flex and need to define real literal numbers, ie. 3.1 or 3.0e-10 as acceptable numbers. Here is what I have so far:
digit [0-9]
int {digit}+
real_literal ({digit}+)("."{digit}*)
From my understanding this works for decimals without accepting something like 12.52.23
How would I define numbers that accept scientific notation, such as 3.0e-10 like mentioned above?
Would it be something like this?
real_literal ({digit}+)("."{digit}*)|({digit}+)("."{digit})[Ee]["-""+"]{digit}+
A possible solution, a bit shorter than yours, would be:
{digit}+"."{digit}*([eE][-+]?{digit}+)?
Break down
{digit}+"."{digit} matches positive real numbers (it won't recognize integers).
(...)? enclosing the next part in parenthesis followed by a question mark makes what's inside optional.
[eE][-+]?{digit}+ match the exponent. Note that the - is not escaped within squared brackets in this case because it's the first or last character of the list, as #rici pointed out.
I just want to point out that you're not recognizing negative numbers here, but I don't know if that's intentional.

Regex for All Digits but Not Exclusively Zeros

I'm trying to find a regex that matches inputs that:
are non-empty AND
are not exclusively zeros (although leading zeros are fine) AND
have no non-digits
Put another way, the string must consist entirely of digits, but not exclusively zeros.
You could use a couple lookaheads:
^(?!0*$)(?=\d+$)
https://regex101.com/r/fTgsWE/3/
Or speed version (avoid look around altogether )
^\d*[1-9]\d*$

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

Does regular expression \d match minus sign and/or decimal point?

I'm look at some old PERL/CGI code to debug an issue and noticed a lot of uses of:
\d - Match non-digit character
\D - Match digit character
Most online docs mention that \d is the same as [0-9], which is what I've always thought of it as. But, I've also noticed Stackoverflow Questions that mention character set difference.
Does "\d" in regex mean a digit?
Does \d also match a minus sign and/or decimal point?
I'm off to do some testing.
Does \d also match a minus sign and/or decimal point?
NO
I don't know how Perl determine whether to use Unicode or ASCII or locale by default (no flag, no use). Regardless, by declaring use re '/a'; (ASCII), or use re '/u'; (Unicode), or use re '/l'; (locale), you will clearly signify to the Perl interpreter (and human reader) which mode you want to use and avoid unexpected behaviour.
Due to the effect of modifiers, \d has at least 2 meanings:
Under effect of /a flag (ASCII), \d will match digits from 0 to 9 (no more and no less).
Under effect of /u flag (Unicode), \d will match any decimal digit in any language, and is equivalent to \p{Digit}reference. This effectively makes \d+ pretty useless and dangerous to use, since it allows a mix of digits in any languages.
Quote from description of /u flag
And, \d+ , may match strings of digits that are a mixture from different writing systems, creating a security issue. num() in Unicode::UCD can be used to sort this out. Or the /a modifier can be used to force \d to match just the ASCII 0 through 9.
\d will not match any sign or punctuation, since those characters does not belong to Nd (Number, decimal digit) General Category of Unicode.
The answer is no. It merely does a digit check. However, Unicode makes things a bit more complex.
If you want to make sure something is a number -- a decimal number -- ake a look at the Scalar::Util module. One of the functions it has is look_like_number. This can be used to see if the string you're looking at could be a number or not, and works better than trying to use a regular expression.
This module has been part of standard Perl for a while, so you should have it on your system.

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

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.