Regex of max 5 decimal separate by + - html

i'm trying to make a regex to add to a input pattern (HTML) to check if is valid,
it need to be valid only if the input contain a string composed by decimal(with 1 or 2 number after comma) or integer number separated by a +
and maximum of 5 number
and it can not start or end with a + or it can not be possible to have a number with comma without number after (i use comma instead of dot for decimal)
for example
10+5,1+6,20 OK
10 OK
6+4+8,9+3+9+3 NO
10,2+4+6+ NO
10,+5 NO
i've tried with something like this but id doesn't work very well
((\d{1,3}|(\d*,\d{1,2})*)+(\+)?){1,5}
also i've tried with this:
^((\s*)|([0-9]\d{0,9}(\,\d{1,2})?%?))*(\+((\s*)|([0-9]\d{0,9}(\,\d{1,2})?%?))+){0,4}$
but it doesn't work very well with the 2 digit max for the decimal and ending +
any suggestions ??
i've made some test here:
https://regexr.com/5jsfv
it should pass the first 3 and faile on the last 4
thanks

You can use
^\d+(?:,\d{1,2})?(?:\+\d+(?:,\d{1,2})?){0,4}$
In the HTML pattern attribute use it as
pattern="\d+(?:,\d{1,2})?(?:\+\d+(?:,\d{1,2})?){0,4}"
See the regex demo.
NOTE: If you want to limit the number of digits in the integer part to be max 3, replace the \d+ with \d{1,3}:
^\d{1,3}(?:,\d{1,2})?(?:\+\d{1,3}(?:,\d{1,2})?){0,4}$
Details:
^ - start of string (implicit in pattern regex)
\d+(?:,\d{1,2})? - one or more digits and then an optional sequence of a , and one or two digits
(?:\+\d+(?:,\d{1,2})?){0,4} - zero to four occurrences of a + char followed with one or more digits and then an optional sequence of a , and one or two digits
$ - end of string (implicit in pattern regex)

Related

Finding numbers with a specific length from csv file

I'm working with a csv file from a customer, which holds a large amount of data. The data is extracted from an SQL database and the commas therefore signify the different columns. In one of these columns there are 10 digit numbers. For some reason all 10 digit numbers starting with 0 have been converted to 9 digit numbers with the 0 removed. I need to find all these instances and insert a 0 at the beginning of the 9 digit number.
A complication in the data is that another column also contains 9 digit numbers, and these do not need to be modified. I can assume, however that all those numbers start with 0 and all the numbers i need to find do not start with 0.
I'm currently using notepad++ trying to fix the problem and found the regular expression \d{9} which finds all numbers with 9 digits, but that is not what I'm looking for
Below i have an example of how the data could look. The column that needs all 9 digit numbers converted is on the left, and the other column with 9 digit numbers is on the right.
An example of the data that is causing the trouble could be:
Column 1
Column 2
2323232323
002132413
231985313
004542435
In this example I need to find the second line of column 1 and insert a 0 in front of the number.
Ctrl+H
Find what: \b(?!0)\d{9}\b
Replace with: 0$0
TICK Wrap around
SELECT Regular expression
Replace all
Explanation:
\b # word boundary, make sure ae haven't digit before
(?!0) # negative lookahead, make sure the next character is not 0
\d{9} # 9 digits
\b # word boundary, make sure ae haven't digit after
Replacement:
0 # 0 to be inserted
$0 # the whole match (i.e. 9 digts)
Screenshot (before):
Screenshot (after):
Using Notepad++ do CTRL + H (search and replace utility).
Tick Regular Expression
Find what ? ([^0-9])(\d{9})([^0-9])
Replace with ? \10\2\3
Explanation :
([^0-9])(\d{9})([^0-9]) matches a 9 digit number surrounded by a non-digit on each side (including line return / comma, etc) :
Each (....) "captures" a group for later use (in "replace").
[^0-9] is a non-number character
\d{9} is a 9 digits number
\10\2\3 is a 0 right after the first captured group \1 (it was just one character here) followed by the 9 digit number (2nd captured group : \2) and the character that was after that number (3rd captured group : \3).
Limit :
It won't match a number at the very beginning of the file (before any other character) or at the very end (after every character). Adding a newline at the end of the file is one workaround, or fixing the last number manually if there is no newline before EOF.

Allow only 1 letter and unlimited numbers in input

So, banging my head on the wall with a regex test site and can't seem to nail this one down.
Trying to make it so an HTML is validated to only allow 1 letter maximum, but unlimited numbers, no other characters.
W123 = valid
124X = valid
1234 = valid
WW12 = invalid
Nothing incredible here, only two possible cases:
the string starts with a digit
the string doesn't start with a digit
In the two scenari only one letter is allowed.
To express that you need a group and an alternation:
^(?:\d+[A-Z]?|[A-Z]\d)\d*$
In the two branches stored inside the group, you can see that there's at least one digit (in the first because I used the + quantifier, in the second because of \d). The first branch matches any string that starts with a digit with and without a letter (because of the optional letter [A-Z]?), the second one matches strings that start with a letter. \d* at the end of the pattern matches remaining digits.
Obviously the pattern is enclosed between anchors for the start ^ and the end of the string $.
You look for zero or more digits followed by a letter followed by zero or more digits or alternatively at least one digit.
^\d*[a-zA-Z]\d*$|^\d+$

Validating using HTML5 pattern

I need to validate two possible patterns for the input using HTML5 pattern.
123456789a (first 9 digits should be exactly numbers and then an alphabetical character) nothing more nothing less
OR
123456789012 (exactly 12 digits nothing more nothing less)
I tried ^([0-9]{12,12})|([0-9]{9,9}[A-Za-z]{1,1}), ^([0-9]{12})|([0-9]{9,9}[A-Za-z])$, and many more but the problem is if user enters an alphabet character when the total length is between 9 and 12, then it takes as a valid input. But it should be not.
Valid input is either 12 digits, or 9 digits with one char.
What have I done wrong?
You could check for 9 digits at the start of the string: (see ^, beginning of input assertion, \d, digit character class and the x{n} quantifier)
^\d{9}
followed by either an alphabetical character or 3 more digits, and the end of the string: (see the non capturing group (?: ... ), [ ... ], the character set, x|y and $, end of input assertion)
(?:[a-zA-Z]|\d{3})$
So the expression would be:
^\d{9}(?:[a-zA-Z]|\d{3})$

Regex for float(x,y) possible?

In the following, float(x,y) means a number with x total digits and y decimals.
I am trying to do client side validation for an HTML Input field. The field corresponds to a MySQL column with data type float(x,y). I know I can define a pattern like float(5,2) with lots of 'ORs'. Is there an efficient way of generating a Regex for this such that I can encode it in my web document?
Something of a workaround is to specify \d+(\.\d{1,y})? and then set maxlength=x+1. Should be x+1 because the decimal place is counted. This would then allow the submit of an integer of length x+1 contrary to specification. I realize I can do JavaScript validation but I would like to achieve the desired with HTML validation.
You can first check the total length with a lookahead and then check for the length of numbers after the decimal point.
If you want X total number of digits and at most Y decimal points, you can use:
^(?=.{X+1}$)([1-9]\d*|0)\.\d{1,Y}$
Explanation:
^ asserts you are in the start position of the line
(?= lookahead (zero length) match for:
.{X + 1} X+1 characters
$ end of line //now you have asserted the total length
the whole part either
0 is a single zero
[1-9]\d* more than a single digit and does not start with zero
\. a dot for the decimal point
\d{1,Y} at least 1 and at most Y digits
$ asserts end of line
Note that you do not need to check the length of the whole part since you are already checking for the total length and the length of digits after the decimal so the part before the decimal point is automatically correct.
Example:
For X = 5 and Y = 2, you will have:
^(?=.{8}$)([1-9]\d*|0)\.\d{1,2}$
Regex101 demo

How to combine two input patterns in HTML?

I need to combine two input patterns into one.
First symbol contains 1: ^[1]\d*$
Min, max length contains 8: .{8}
What expression should I use?
Just repeat {7} digits instead of repeating the digits with *, to ensure the string has 8 digits total:
^1\d{7}$