Regex: how to check string contains blocks separated by commas [duplicate] - regex

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 4 years ago.
I need to validate an input field, which should contains blocks separated by commas,and maximum is 50 blocks, each block must be 8 characters long, only number and letter be allowed.
Examples: 1F223142,23FH2324,3232UD23
I searched but I cannot find a matching one, so how should my regex be ?

Try this one.
/([A-Z0-9]{8},){0,49}([A-Z0-9]{8}){1}/g
This will look for (8 capital letters or numbers followed by a comma) for minimum 0 to maximum 49 times. Then it will look for (8 capital letters or numbers followed by a comma) one time.
In this way a user can enter a Single block NOT followed by comma or maximum 50 blocks seperated by commas with the last one NOT followed by a comma.
You will need to compare the lengths of the original input and the result from input. For Example:
let a = "1F223142,23FH2324,3232UD23";
let r = /([A-Z0-9]{8},){0,49}([A-Z0-9]{8}){1}/g.exec(a)[0].length;
if (a.length == r.length) {
//valid input
} else {
//invalid input
}

Related

Multi-part Regular Expression [duplicate]

This question already has answers here:
Comma Separated Numbers Regex
(6 answers)
Closed 3 years ago.
I have the requirement to restrict a non-required textbox to only numbers (0-9) with a separator of ';'. The pattern is that the groups can be 4 or 5 in length and can repeat n times. Anything less than 4 in a group is invalid. After 4 or 5 I need to check for the existence of a separator character ';'. This pattern can repeat n times. I have tried variations of but this doesn't seem to be working. Something simple to start out like
[0-9]{4,5};+
is invalid as I don't need the separator for only 1 number grouping.
Next I tried
^[0-9]{4,5}|[0-9]{4,4};|[0-9]{5,5};$
but this doesn't work because the existence of four digits like 1111 or five digits 11111 before gives one match before it goes awry example "11111;j" Is there a way in a regex to validate
1111
11111
1111;1111
11111;1111
11111;11111
but catch
111
111;
1111;1
11111;1
abc
in a repeating fashion?
This validate your examples.
^[0-9]{4,5}(;[0-9]{4,5})?$
Try it
It's not clare what you mean by "in a repeating fashion". If you want validate also this
1111;11111;11111;1111;11111
You can use this regex
^[0-9]{4,5}(;[0-9]{4,5})*$
Try it

Regex for custom email validation [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 4 years ago.
Trying to create custom email validation for below rules
The local part can be up to 64 characters in length and consist of any combination of alphabetic characters, digits, or any of the following special characters:
! # $ % & ‘ * + – / = ? ^ _ ` . { | } ~
The period character (".") is valid for the local part subject to the following restrictions: A. it is not the first or last character
B. two or more consecutive periods
top level domains cannot be all numeric
hyphens cannot be the first or last character
^([a-zA-Z0-9!#\$%&‘*+/\=\?\^_'`}{\|~-][.]?)#[a-zA-Z0-9]+(?:(.)\0?(?!\1))[a-zA-Z0-9-]*[a-zA-Z0-9]+(.[a-zA-Z0-9]{2,63})+$
First part (before # )is good but unable to place
two or more consecutive periods
hyphens cannot be the first or last character
for example
leela.test#te-st.gm-ail.com(correct)
leela.test#te-st..gm-ail.com(incorrect)
leela.test#.te-st.gm-ail.com(incorrect)
leela.test#-te-st.gm-ail-.com(incorrect)
leela.test#.te-st.gm-ail-.com(incorrect)
leela.test#test.gmail.com(correct)
leela#gmail.com(correct)
leela#test.gm-ail.com(correct)
Please help.
[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*\#[a-zA-Z0-9]+\-[A-Za-z0-9]+\.[a-zA-Z0-9]+\-[A-Za-z0-9]+\.com

How to write regular expression which accept numbers between 1 to 25000 [duplicate]

This question already has answers here:
Using regular expressions to validate a numeric range
(11 answers)
Closed 4 years ago.
How to write regular expression which accept numbers between 1 to 25000>
I tried like this ^([1-2]?[1-4]?[0-9]{0,3}|25000)$
Here's a regex that will only accept a string with a number between 1 and 25000.
Without proceeding zero's.
^([1-9]\d{0,3}|1\d{4}|2[0-4]\d{3}|25000)$
It basically separates it in 4 ranges
[1-9]\d{0,3} : 1 to 9999
1\d{4} : 10000 to 19999
2[0-4]\d{3} : 20000 to 24999
25000 : 25000
A regex101 test can be found here
To find those numbers as a part of a string, you could replace the start ^ and end $ by a wordboundary \b.
Btw, in most programming languages it's often simpler to just check if it's a number that's in the accepted range. Even in HTML there's an input type for numbers where you can set the minimum and maximum.
Try
^(?!0$)((1\d|2[0-4]|\d)?\d{1,3}|25000)$
First negative lookahead will reject a value of only 0.
The group (1\d|2[1-4]|\d)? means that a 5-digit number with an initial digit of 2 requires it to be followed by a 0-4.
https://regex101.com/r/1DgbBM/4

Regex array of number [duplicate]

This question already has answers here:
Regex validation for numbers with comma separator
(3 answers)
Closed 4 years ago.
I have a problem with regex and need your help. I want to check my string is correct or incorrect. First and last is a number, only number and comma after it. No space inside 2 numbers.
Ex:
1,2,3,49,5 this is correct
1,2,3,45, this is incorrect
,12,4,2,67 this is incorrect
1,2 3,4,5,6 this is incorrect
^(?:\d+,)*\d+$
(?:\d+,)* - gets matches like "0," "00," "000," ... or empty
\d+ - gets last number as "0" "00" "000"
Please check below regex to solve your problem.
Regex: ^[0-9]+([0-9,])+[0-9]+$
^[0-9]+ is for start with one or more number
[0-9]+$ is for end with one or more number
([0-9,])+ is for one or more number with comma
Please check the output in Regex101
Update:
Please check the updated regex: ^(\d+,)+\d+$
^(\d+,)+ is for one or more number with comma and this will handle first number with comma
\d+$ is for end with one or more number
Please check the updated output in Regex101

Search and validate a year (four continuous digits) in text

How do I validate that a string has only 4 contiguous digits or no digits?
I know that /\d{4}/ will validate 4 contiguous digits, but if my string is dh25ah1233dadh3, it must be invalid.
Valid strings
dkskdsokd
adad dadad
addad1257adada
1587dadad
sasasas7854
Invalid strings
dh25ah1233dadh3
fsdfdfd1982fdf2
1some1422dd
If I understand the question, it'd be something like:
/^(?:\D*\d{4}\D*)*$/
That matches any number of the sub-pattern "some non-digits, then 4 digits, then some more non-digits".
If you want only one group of 4 digits, it'd be
/^\D*(?:\d{4})?\D*$/
edit — if you want the first one, and you don't want groups of 8 (or 12 or 16 ...) digits to be accepted, you'd do something like this:
/^(?:\D*\d{4}\D+)*$/
edit let's try this again:
/^(?:\D*\d{4}\D+)*(?:\D*\d{4})?$/
That allows the string to end with a 4-digit group.