Regex for positive float numbers - regex

For example:
10
0.1
1.23234
123.123
0.000001
1.000
.3
And wrong examples:
0001.2
-12
-1.01
+2.3
EDIT: standart JavaScript regex.

Try this here
^(?:[1-9]\d*|0)?(?:\.\d+)?$
See it here online on Regexr
If matching the empty string is not wanted, then you can add a length check to your regex like
^(?=.+)(?:[1-9]\d*|0)?(?:\.\d+)?$
The positive lookahead (?=.+) ensures that there is at least 1 character

This will pass all your test cases, multi-line mode enabled:
/^(?!0\d)\d*(\.\d+)?$/mg
Explanation:
/^ # start of regex and match start of line
(?!0\d) # not any number with leading zeros
\d* # consume and match optional digits
(\.\d+)? # followed by a decimal and some digits after, optional.
$ # match end of line
/mg # end of regex, match multi-line, global match
RegExr: http://regexr.com?2tpd0

Consider the regular expression:
^[0-9]*(?:\.[0-9]*)?$
This regular expression will matches floating point number like:
- .343
- 0.0
- 1.2
- 44
- 44.
- 445.55
- 56.
- . //Only dot(.) also matches
- empty string also matches
The above regular expression will will not accept:
- h32.55 //Since ^ is used. So, the match must start at the beginning
of the string or line.
- 23.64h //Since $ is used. So, the match must occur at the end of the string or before \n at the end of the line or string.
Consider the regular expression:
^[0-9]+(?:\.[0-9]+)?$
This regular expression will matches floating point number like:
- 45
- 45.5
- 0.0
- 1.2
- 445.55
This regular expression will not accept:
- h32.55 //Since ^ is used. So, the match must start at the beginning
of the string or line.
- 23.64h //Since $ is used. So, the match must occur at the end of the string or before \n at the end of the line or string.
- 44.
- . //Only dot(.) does not matches here
- empty string also does not matches here
Pure floating point:
^(([0-9]+(?:\.[0-9]+)?)|([0-9]*(?:\.[0-9]+)?))$
You can check the regular expression here.
Refer MSDN page for additional information.

I've stumbled on this page a few times, here is my solution for any one who stumbles here after me:
A regex like a=(\d+\.?\d* | \d*\.?\d+) matches all decimals numbers without a sign but includes things like 002.0
A regex to filter those things are b=[1-9\.]+.*
So one solution is to say it matches the criteria if a & b matches. Or equivalently (contrapositive), see if there is no match for !a | !b. Unfortunately, most languages don't have a complete regex package; the 'and' and negate functions of regular languages isn't present usually. Two simple regexes I've found in code looks a lot nicer and are more maintainable than one complex one (I say this in context to this question & similar situations)

Related

Regular Expression to Validate Monaco Number Plates

I would like to have an expression to validate the plates of monaco.
They are written as follows:
A123
123A
1234
I started by doing:
^[a-zA-Z0-9]{1}?[0-9]{2}?[a-zA-Z0-9]{1}$
But the case A12A which is false is possible with that.
You can use
^(?!(?:\d*[a-zA-Z]){2})[a-zA-Z\d]{4}$
See the regex demo. Details:
^ - start of string
(?!(?:\d*[a-zA-Z]){2}) - a negative lookahead that fails the match if there are two occurrences of any zero or more digits followed with two ASCII letters immediately to the right of the current location
[a-zA-Z\d]{4} - four alphanumeric chars
$ - end of string.
You can write the pattern using 3 alternatives specifying all the allowed variations for the example data:
^(?:[a-zA-Z][0-9]{3}|[0-9]{3}[a-zA-Z]|[0-9]{4})$
See a regex demo.
Note that you can omit {1} and
To not match 2 chars A-Z you can write the alternation as:
^(?:[a-zA-Z]\d{3}|\d{3}[a-zA-Z\d]|\d[a-zA-Z\d][a-zA-Z\d]\d)$
See another regex demo.
So it needs 3 connected digits and 1 letter or digit.
Then you can use this pattern :
^(?=.?[0-9]{3})[A-Za-z0-9]{4}$
The lookahead (?=.?[0-9]{3}) asserts the 3 connected digits.
Test on Regex101 here

php check ncr with negative lookbehind and greedy doesn't work

I want to find a erroneous NCR without &# and remedy it, the unicode is 4 or 5 decimal digit, I write this PHP statement:
function repl0($m) {
return '&#'.$m[0];
}
$s = "This is a good 23200; sample ship";
echo "input1= ".htmlentities($s)."<br>";
$out1=preg_replace_callback('/(?<!#)(\d{4,5};)/','repl0',$s);
echo 'output1 = '.htmlentities($out1).'<br>';
The output is:
input1= This is a good 23200; sample ship
output1 = This is a good 2ಀ sample ship
The match only happens once according to the output message.
What I want is to match '23200;' instead of '3200;'.
Default should be greedy mode and I thought it will capture 5-digit number instead 4-digit's
Do I misunderstand 'greedy' here? How can I get what I want?
The (?<!#)(\d{4,5};) pattern matches like this:
(?<!#) - matches a location that is not immediately preceded with #
(\d{4,5};) - then tries to match and consume four or five digits and a ; char immediately after these digits.
So, if you have #32000; string input, 3 cannot be a starting character of a match, as it is preceded with #, but 2 can since it is not preceded by a # and there are five digits with a ; for the pattern to match.
What you need here is to curb the match on the left by adding a digit to the lookbehind,
(?<![#\d])(\d{4,5};)
With this trick, you ensure that the match cannot be immediately preceded with either # or a digit.
You say you finally used (?<!#)(?<!\d)\d{4,5};, and this pattern is functionally equivalent to the pattern above since the lookbehinds, as all lookarounds, "stand their ground", i.e. the regex index does not move when the lookaround patterns are matched. So, the check for a digit or a # char occurs at the same location in the string.

RegEx for matching numbers from 0.00 to 50.00

How can I get an regex expression that allows:
0.00 to 50.00 and also comma as decimal separator so
0,00 to 50,00
I have gotten as far as ([0-5]{1})?([0-9]{1})([,][0-9]{1,2})?
but there are still situations in which it fails. I have searched on line but could not find the right answer.
ADDED:
A small change in requirements. Actually it should be from 0.01 to 50.00, and 0,01 to 50,00. (But with the answers below I think I managed to adapt the regex strings so this is also matched)
There is a regex for range generator here. Generated and played with it, came up with this
^(?:[1-4]?\d[.,]\d\d?|50[.,]00?)$
Added non capturing group, little modifications and ^ start / $ end anchor. See demo at regex101.
For optional decimal part, how about this: ^(?:[1-4]?\d(?:[.,]\d\d?)?|50(?:[.,]00?)?)$
This regex should cover almost all cases matching numbers from 0.00 to 50.00,
^(?=.)(?:(?:(?:0|[1-4]?\d)?(?:[,.]\d{1,2})?)|50(?:[.,]00?)?)$
Explanation:
^ - Start of string
(?=.) - Positive look ahead to avoid matching empty string
(?: - Start of first non-grouping pattern
(?: - Start of second non-grouping pattern
(?:0|[1-4]?\d)? - This matches whole number from 0 to 49 and this whole number could be absent
(?:[,.]\d{1,2})? - Matches a comma or dot followed by one or two digits and this decimal part can be absent
) - Closing of second non-grouping pattern
| - Alternation for number 50 case
50(?:[.,]00?)?) - Matches 50 followed by either comma or dot followed by 0 or 00 where decimal part is optional
) - Closing of first non-grouping pattern
$ - End of string
Regex Demo
Edit:
For discarding zero value numbers, you can just add a negative lookahead (?!0*[.,]?0*$) in current regex and use this regex,
^(?=.)(?!0*[.,]?0*$)(?:(?:(?:0|[1-4]?\d)?(?:[,.]\d{1,2})?)|50(?:[.,]00?)?)$
Regex Demo rejecting zero valued numbers
does this one work for you?
(50([.,]0{1,2})?)|([0-4]?[0-9]([.,][0-9]{1,2})?)

Regex compare whole number and ignore separators

I would like to match the following pattern
1.XXXXXX.XXX.X
The combination must begin with a 1 and must contain at least a second number greater than 0 somewhere. Only numbers and points allowed.
So the following examples would be correct
1.000000.000.1
1.000500.000.0
1.020030.030.0
And the following examples would be incorrect
1.000000.000.0
1.0000.00.0
1.0000d0.020.0
What I have created so far
(?=^[1][\.][0-9]{6}[\.][0-9]{3}[\.][0-9]{1}$) // check pattern 1.XXXXXX.XXX.X
(?=^[1-9](?!0000000000$)[0-9][1-9]?\d+$) // check if input is greater than 10000000000
Unfortunately, the second statement does not work because of the points in the input. Is it possible to read the complete number and ignore the points or is there a better solution?
You may use
^1\.(?!(?:\.?0)+$)\d{6}\.\d{3}\.\d$
See the regex demo
The regex will fail the match if there are only zeros and dots after the initial 1..
Details
^1\. - 1. at the start of the string
(?!(?:\.?0)+$) - a negative lookahead that will fail the match of there are one or more sequences of an optional . and a zero up to the end of the string
\d{6} - 6 digits
\. - a dot
\d{3} - 3 digits
\. - a dot
\d - a digit
$ - end of string.

Regex for Chilean RUT/RUN with PCRE

I'm having issues with the validation of the chilean RUT/RUN with a regex expression in PCRE. I have the next regular expression but sadly can't make it work:
\b[0-9|.]{1,10}\-[K|k|0-9]
I need help to see what is wrong with the code. The application I need to use only uses PCRE.
Thank you.
You may use
^(\d{1,3}(?:\.\d{1,3}){2}-[\dkK])$
to match and capture (that is not usually necessary, but your app requires a capturing group to extract its contents) a whole string that matches the pattern. See the regex demo.
To match shorter strings that match this pattern inside a larger string, you may remove ^ and $ (see demo) or use \b word boundaries instead (see this demo).
Details:
^ - start of string
\d{1,3} - 1 to 3 digits
(?:\.\d{1,3}){2} - 2 sequences of a literal . and 1 to 3 digits
- - a hyphen
[\dkK] - a digit, k or K.
$ - end of string.
As they sometimes omit the dots, I used this one:
^(\d{1,2}(?:[\.]?\d{3}){2}-[\dkK])$
Details:
^ - start of string
\d{1,2} - 1 or 2 digits
(?:[.]?\d{3}){2} - 2 sequences of an optional '.' and 3 digits
- a hyphen
[\dkK] - a digit, k or K
$ - end of string
1234567-k OK
12345678-k OK
1.234.567-k OK
12.345.678-k OK
known issue:
12.345678-k and 12345.678-k still OK and I do not like this :(
You need to change to ^(\d{1,3}(?:\.\d{3}){2}-[\dkK])$ to capture only 2 sequence of 3 digits after the first sequence of 1-3 digits.
please consider being more specific in the REGEX build, since it matched wrong numbers, such as 17.87.335-2. Also the included one did't match formats without the dots or the hyphens.
Please consider using the following format: \b(\d{1,3}(?:(.?)\d{3}){2}(-?)[\dkK])\b
Modified prior version to try the other formats: https://regex101.com/r/2Us0j6/9