i need to validate a reference code that is in the form of 2 letters, 3 numbers and one letter. I've tried using for functions, and have also tried going through each letter individually but its either too messy or it's not giving me the result i want. I've heard you can use regex but i'm not sure how to apply it. Any help would be great. Thanks
I was told that this works:
reference.matches("[A-z]{2}+[0-9]{3}+[A-z]")
but when i run it, no matter what i put into the console that is is incorrect.
that is in the form of 2 letters, 3 numbers and one letter
You can use this regex:
reference.matches("(?i)[A-Z]{2}[0-9]{3}[A-Z]");
Explanation:
(?i) - for ignore case matching
[A-Z]{2} - for matching 2 letters
[0-9]{3} - for matching 3 digits
[A-Z] for matching single letter
Related
I'm having some trouble creating a regex for a password. It has the following requirements :
at least 10 characters
at least 1 uppercase letter
at least 1 lowercase letter
at least 1 special character
I currently created this line :
`
"^(?=(.*[A-Z])+)(?=(.*[a-z])+)(?=(.*[0-9])+)(?=(.*[!##$%^&*()_+=.])+){10,}$"
`
For a password like : Lollypop56#
it still gives me false.
You forgot a full point after the lookahead of special characters. So it would be:
"^(?=(.*[A-Z])+)(?=(.*[a-z])+)(?=(.*[0-9])+)(?=(.*[!##$%^&*()_+=.])+).{10,}$"
I recommend to you that use https://www.passay.org/. This dependency able you to validate wide range of password policy.
I'd remove the + in the groups inside the lookaheads since those aren't needed and also make those groups non-capturing. I'd also not explicitly specify the characters in the "special" group. Just make it match any of the characters not in the first three groups, [^A-Za-z0-9]. That'll allow ~ as a special character too etc.
Also, the actual matching should be .{10,}, not {10,}.
"^(?=(?:.*[A-Z]))(?=(?:.*[a-z]))(?=(?:.*[0-9]))(?=(?:.*[^A-Za-z0-9])).{10,}$"
Can anyone tell how I can write regex for a string that take one or more alphanumeric character followed by an even number of digits?
Valid:
a11a1121
bbbb11a1121
Invalid:
a11a1
I have tried ^[a-zA-Z*20-9]*$ but it is always giving true.
Can you please help in this regard?
The regex that you have mentioned will search for any number of [either a-z, or A-Z or 2 or 0-9]
You can break down your requirement to groups and then handle it accordingly.
Like you require at least one character. so you start with ^([a-zA-Z]+)$
Then you need numbers in the multiple of 2. so you add ^([a-zA-Z]+(\d\d)+)$
Now you need any number of combination of these. So the exp becomes: ^([a-zA-Z]+(\d\d)+)*$
You can use online tools like regex101 for these purpose. The provided regex in action here
You can achieve it with this regexp: ^[a-z0-9]*[a-z]+([0-9]{2})*$
Explanation :
[a-z0-9]*[a-z]+: a string of at least one character terminated by a non digit one
([0-9]{2})*: an odd sequence of digits (0 or 2*n digits). If the even sequence cannot be null, use ([0-9]{2})+ instead.
I am trying to figure out a regex to match a password that contains
one upper case letter.
one number
one special character.
and at least 4 characters of length
the regex that I wrote is
^((?=.*[0-9])(?=.*[A-Z])(?=.*[^A-Za-z0-9])){4,}
however it is not working, and I couldn't figure out why.
So please can someone tell me why this code is not working, where did I mess up, and how to correct this code.
Your regex can be rewritten as
^(
(?=.*[0-9])
(?=.*[A-Z])
(?=.*[^A-Za-z0-9])
){4,}
As you see {4,} applies to group which doesn't let you match any character since look-around is zero-width, which effectively means "4 or more of nothing".
You need to add . before {4,} to let your regex handle "and at least 4 characters of length" point (rest is handled by look-around).
You can remove that capturing group since you don't really need it.
So try with something like
^(?=.*[0-9])(?=.*[A-Z])(?=.*[^A-Za-z0-9]).{4,}
You could come up with sth. like:
^(?=.*[A-Z])(?=.*\d)(?=.*[!"ยง$%&/()=?`]).{4,}$
In multiline mode, see a demo on regex101.com.
This approach specifies the special characters directly (which could be extended, obviously).
From the following list only the bold ones would satisfy these criteria:
test
Test123!
StrongPassword34?
weakone
Tabaluga"12???
You can still enhance this expression by being more specific and requiring contrary pairs. Just to remind you, the dot-star (.*) brings you down the line and then backtracks eventually. This will almost always require more steps than to directly look for contrary pairs.
Consider the following expression:
^ # bind the expression to the beginning of the string
(?=[^A-Z\n\r]*[A-Z]) # look ahead for sth. that is not A-Z, or newline and require one of A-Z
(?=[^\d\n\r]*\d) # same construct for digits
(?=\w*[^\w\n\r]) # same construct for special chars (\w = _A-Za-z0-9)
.{4,}
$
You'll see a significant reduction in steps as the regex engine does not have to backtrack everytime.
I am trying to use this regex
.*[!##$%^&*()].*[0-9]|[0-9].*[!##$%^&*()].*
to make my passwords have at least 1 special character and 1 number (which it does). But I am trying to also have it take passwords like mike1# .
How can I modify it so it takes letters like that as well?
Its better to do this using lookaheads.
^(?=.*[!##$%^&*()])(?=.*[0-9])(?=.*[a-zA-Z]).*$
The problem with your approach is you will have to cover all possible combinations where in a letter comes first,or a number comes first.Using lookahead which are 0 width assertions are just a type of check,we can fail the regex if it does not have even a single special character or number or letter.
(?=.*[a-zA-Z])
^^ ^^^^^^^
The lookahead simply states that for this regex to pass there should be at least one letter somewhere.
EDIT:
^(?=.*[!##$%^&*()])(?=.*[0-9]).*$
Use this if letter constraint is not present.
Can I use Reg Expression for the following use case?
I Need to write a boolean method which takes a String parameter that should satisfy following conditions.
20 character length string.
First 9 characters will be a number
Next 2 characters will be alphabets
Next 2 characters will be a number.(1 to 31 or 99)
Next 1 character will be an alphabet
Last 6 characters will be a number.
In this, I have wrote the code for the first requirement:
[a-zA-Z0-9]{20} - This expression works well for the first case. I don't know how to write a complete reg expression to meet the entire requirement.
Please help.
Yes, it is possible to use regexes for this.
Ignore the "20 characters" part and describe a string created by concatenating 9 digits, 2 letters, 2 digits, 1 letter and another digit.
Start with the string start: ^
Then 9 digits. The \d conveniently describes the character set [0-9], so \d{9} means "nine digits"
Then 2 letters. The \w class is too broad, so stick to [a-zA-Z] for a letter.
Then another two digits. They seem to be from a restricted set, so describe the set with alternation and grouping.
Then another letter and another digit.
And, finally, you have to end at the end of the string: $
For reference, this regex means "the string is nine letters, then 12-15 or 99, then another letter":
^[a-zA-Z]{9}(1[2-5]|99)[a-zA-Z]$
Read the String JavaDocs, especially the part about String.matches() as well as the documentation about regular expressions in Java.
Your first requirement is already implicit in the remaining ones, so I would just skip it. Then, just write the regex code that matches each part one after the other:
[0-9]{9}[a-zA-Z]{2}...
There is one special consideration for the number that might be 1 to 31. While it is possible to match this in one regex, it would be verbose and difficult to understand. Instead, perform basic matching in the regex and extract this part as a capturing group by putting it into parentheses:
([0-9]{2})
If you use Pattern and Matcher to apply your regex, and your string matches the pattern, you can then easily get at just thost two characters, use Integer.parseInt() to convert them to an integer (which is completely safe because you know the two characters are digits), and then check the value normally.
This regular expression takes
^[0-9]{9}[a-zA-Z]{2}([1-9]|[1-2][0-9]|3[0-1]|99)[a-zA-Z]([0-9]{6})$
takes
9 letters at start,
Followed by 2 alphabets,
Followed by number between 1 to 31 or 99,
Followed by an alphabet,
followed by 6 digits.