regex to check for subsequent characters [duplicate] - regex

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

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).

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 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

Regex Help - Named capture group that ends in two possible ways [duplicate]

This question already has answers here:
How to extract a value from a URL query string in C#?
(4 answers)
Closed 4 years ago.
I'm trying to capture a group that has a variety of different forms.
cardType=A&Return=True
cardType=AbC321
Return=False&cardType=C
My current regex is:
cardType=(?<Card Type>.*)&?
This currently captures the 2 and 3, but not in 1 as it also captures Return in that case.
If I do instead:
cardType=(?<Card Type>.*?)&
Then it correctly captures 1, but not 2 and 3.
How do I write a regex that captures it in all 3 cases?
Use:
cardType=(?<CardType>[^&\s]*)
Demo
Two important changes:
removed space form group name
replaced .*)&? with [^&\s]*)
The second change is more important. When you do .*&?, then &? is never captured, because .* takes the & sign. As in most cases it's better to limit the repetition by limiting scope of accepted charactes to [^&\s] - anything but whitespace or ampersand

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