Regex for custom email validation [duplicate] - regex

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

Related

Extracting text with no consecutive whitespaces [duplicate]

This question already has answers here:
Python regex to parse semi-fixed width file
(4 answers)
Split string into a list on whitespace, excluding single spaces when the next character is not a dash
(2 answers)
Closed 2 years ago.
I have a sample input below and I would like to extract each individual column using regex but it cant work for multiple consecutive blank spaces. I've tried
"([0-9])\s+([0-9])\s+([A-Za-z0-9- ]+)\s{2,}([A-Za-z0-9- ]+)\s+([A-Za-z0-9]+)" and it should work for each row.
Output
Module Ports Type Model Serial No.
--------- ----- ------------------------------------ --------------- -----------
1 2 CCS-7354 Series Supervisor Module 7354-SPP JD546546527
2 1 Standby supervisor Unknown Unknown
3 28 28-port SFP+ 10GigE Linecard 7234S-PC FGK10449938
For the first row of the input result, I should get:
"1" for "Output Module".
"2" for "Ports"
"CCS-7354 Series Supervisor Module" for "Type".
"7354-SPP" for Model.
"JD546546527" for "Serial No."
I'm getting "CCS-7354 Series Supervisor Module 7354-SPP " for the Type which is incorrect.
Your problem is that the Type column match group [A-Za-z0-9- ]+ uses a "greedy" match.
Instead you should change it to a "reluctant" match [A-Za-z0-9- ]+?
Likewise, the Model column match group after that should also be changed to a reluctant match instead of a greedy match, so that it won't preemptively eat up all its trailing spaces.
Here is the final regex -- ([0-9])\s+([0-9])\s+([A-Za-z0-9- ]+?)\s{2,}([A-Za-z0-9- ]+?)\s+([A-Za-z0-9]+)
Test here: link
Of course there are other ways you could write the regex such that you wouldn't need to use a reluctant match syntax. For example ((?:\S|\s\S)+)
This matches non-space characters separated by at most one whitespace character.
And putting it all together, it would be: ([0-9])\s+([0-9])\s+((?:\S|\s\S)+)\s+((?:\S|\s\S)+)\s+((?:\S|\s\S)+)
Writing it this way reduces the amount of potential backtracking and should thus result in a consistently fast regex, regardless of input (although with this simple input it appears to be marginally slower).

Regular expressions in Postgres [duplicate]

This question already has answers here:
Match exact string
(3 answers)
Closed 3 years ago.
all.
I'm querying a Postgres 9.3 database looking for a specific pattern in a field:
SELECT
P.id, P.processo_id, PR.num_formated
FROM
publications P
INNER JOIN processos PR ON PR.id=P.processo_id
WHERE
--The first numeric sequence must be exact 4 digits length, so the initial \d{4}
PR.num_formated ~ '\d{4}[\.\-\\]\d{2}\.\d{4}\.\d{1}\.\d{2}\.\d{4}'
AND
P.id=781291700 --Just to force a specific record
where PR.num_formated is defined as "character varying(255)". After being executed, the query returns:
P.id P.processo_id PR.num_formated
781291700 502707245 20190001418-14.1998.8.05.0103
My question is: Why is Postgres "ignoring" the first \d? Is there any specificity in the form it interprets the regular expressions that differ from the "traditional/regular/orthodox/whatever" way, since the same regex works perfectly in another part of my system, but using a ruby code?
Thanks in advance
Walid
The first 7 chars are ignored because the query finds the pattern as a part of the string. If you want to match the whole string use ^ and $ constraints.
'^\d{4}[\.\-\\]\d{2}\.\d{4}\.\d{1}\.\d{2}\.\d{4}$'

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

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
}

regex to check for subsequent characters [duplicate]

This question already has answers here:
What's a Regex pattern for 3 consecutive digits increasing or decreasing
(3 answers)
Closed 6 years ago.
i would like to ask if it is possible to get a regex to check for subsequent alphabets or numbers in a string such as "abcd" or "1234" 4 characters in succession.
This is for password validation to check if a user is trying to enter a password like "abcd1234"
Thank You
Based on the answer Match increasing/decreasing sequences using regex, this pattern does close. Through programming you still need to get the first capturing group \1 and check whether its size is equal to 4. Here you need to set the multi line mode, to allow $ match the end of line.
This is because this regex is capturing all possible groups, however the groups you want has the length of 4, so it should not be a problem.
(?x)
(
(?:a(?=b|$))?
(?:b(?=c|$))?
(?:c(?=d|$))?
(?:d(?=e|$))?
(?:e(?=f|$))?
(?:f(?=g|$))?
(?:g(?=h|$))?
(?:h(?=i|$))?
(?:i(?=j|$))?
(?:j(?=k|$))?
(?:k(?=l|$))?
(?:l(?=m|$))?
(?:m(?=n|$))?
(?:n(?=o|$))?
(?:o(?=p|$))?
(?:p(?=q|$))?
(?:q(?=r|$))?
(?:r(?=s|$))?
(?:s(?=t|$))?
(?:t(?=u|$))?
(?:u(?=x|$))?
(?:x(?=z|$))?
[a-z]?
|
(?:0(?=1|$))?
(?:1(?=2|$))?
(?:2(?=3|$))?
(?:3(?=4|$))?
(?:4(?=5|$))?
(?:5(?=6|$))?
(?:6(?=7|$))?
(?:7(?=8|$))?
(?:8(?=9|$))?
\d?
)
On this link you can see the live regex: https://regex101.com/r/xxED4s/2

Regular Expression for US and other format zip [duplicate]

This question already has answers here:
What is the ultimate postal code and zip regex?
(20 answers)
Closed 9 years ago.
Am working on regular expression for following formats of zip in groovy
Includes a letter (L12345)
Includes a dash plus 4 more numbers (77056-1234)
Includes spaces (77056 1234)
I have this "^\d{5}(-\d{4})?\$" but its not matching the required formats. Could anyone please help me?
^\d{5}(?:[-\s]\d{4})?$
^ = Start of the string.
\d{5} = Match 5 digits (for condition 1, 2, 3)
(?:…) = Grouping
[-\s] = Match a space (for condition 3) or a hyphen (for condition 2)
\d{4} = Match 4 digits (for condition 2, 3)
…? = The pattern before it is optional (for condition 1)
$ = End of the string.
This is from the following question, hope it helps
regex for zip-code
For the optiona startingil letter use
[A-Z]?
to make the letter optional. {1} is redundant. (Of course you could also write [A-Z]{0,1} which would mean the same, but that's what the ? is there for.)
I think it should go after the ^ but haven't had a chance to test