How to allow set of special characters in existing regex? - java

We have regex to validate password with one digit, one upper case letter and one lower case letter. Regex is:
^\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])\w*$
This regex will not allow any special characters. I need to change regex to allow some list of special characters and there should not be any restriction that there must be at least one special character. Only [-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/] should be allowed as special characters without must have one restriction.
I tried:
^\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])(?=\w*[-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]*)\w*$
and which seems to be wrong. Please some one help.

This is because of \w* before $.You are specifically trying to match 0 to many words..Try this:
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[-\w!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]+$
OR
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[\w\p{Punct}]+$
\p{Punct} is a special character class similar to [!"#$%&'()*+,\-./:;<=>?#[\\\]^_{|}~]

Related

I'm trying to create a regex for a strong password

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,}$"

Regex + sign followed by numbers

Hi i want to find Strings like "+19" in Java
so a + sign followed by infinite amount of numbers.
How do i do this?
Tried "+[0123456789]"
and "\+[0123456789]"
thank you :)
This is the regex you want to use:
\\+\\d+
Two kinds of plus are being used here. The first is escaped with two backslashes because it is treated as a literal. The second one means match 1 of more times (i.e. match any digit one or more times).
Code:
String input = "+19";
if (input.matches("\\+\\d+")) {
System.out.println("input string matches");
}
Yes, to match a plus you need to escape it with two backslashes in a C string literal that Java uses. A literal plus needs to be either escaped or put into a character class, [+]. If you just use a plus symbol, it becomes a quantifier that matches the previous symbol or group one or more number of times.
Also, note that the \d shorthand digit class can match more than just ASCII digits if Pattern.UNICODE_CHARACTER_CLASS flag is passed to Pattern.compile (or embedded (?U) flag is added at the start of the pattern). It is advised to use unambiguous patterns in case the code might be maintained or enhanced/adjusted by different developers later.
Most people prefer patterns without escaping backslashes if possible since that allows to avoid issues like the one you faced.
Here is a version of the regex that does not require any escaping:
"[+][0-9]+"
Also, the plus quantifier does not match an infinite number of digits, only MAX_UINT number of times.

regex not working for password

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.

Java Regular Expression Pattern Matching

I want to create a regular expression, in Java, that will match the following:
*A*B
where A and B are ANY character except asterisk, and there can be any number of A characters and B characters. A(s) is/are preceded by asterisk, and B(s) is/are preceded by asterisk.
Will the following work? Seems to work when I run it, but I want to be absolutely sure.
Pattern.matches("\\A\\*([^\\*]{1,})\\*([^\\*]{1,})\\Z", someString)
It will work, however you can rewrite it as this (unquoted):
\A\*([^*]+)\*([^*]+)\Z
there is no need to quote the star in a character class;
{1,} and + are the same quantifier (once or more).
Note 1: you use .matches() which automatically anchors the regex at the beginning and end; you may therefore do without \A and \Z.
Note 2: I have retained the capturing groups -- do you actually need them?
Note 3: it is unclear whether you want the same character repeated between the stars; the example above assumes not. If you want the same, then use this:
\A\*(([^*])\2*)\*(([^*])\4*)\Z
If I got it correct.. it can be as simple as
^\\*((?!\\*).)+\\*((?!\\*).)+
If you want a match on *AAA*BBB but not on *ABC*DEF use
^\*([a-zA-Z])\1*\*([a-zA-Z])\2*$
This won't match on this either
*A_$-123*B<>+-321

regex pattern java symbols

I am looking for a regex pattern in Java that corresponds to all characters except the letters a to z.
In other words, I want a regex pattern that corresponds to symbols such as
!"#¤%&/()=?`´\}}][{€$#
Or some way to trim a string into letters only.
As an example lets consider the following string:
"one!#"¤%()=) two}]}[()\ three[{€$"
to:
"one two three"
The Unicode version would be
\PL
\PL are all Unicode code points that does not have the property "Letter".
\pL would be the counterpart, all Unicode code points that does have the property "Letter".
Maybe you can fine here on regular-expressions.info some properties that match your needs better.
You can also combine them into character classes, the same than you would handle predefined classes, e.g.
[^\pl\pN]
Would match any character that is not a letter or a digit numeric character in Unicode.
As an example lets consider the following string:
"one!#"¤%()=) two}]}[()\ three[{€$"
to:
"one two three"
The pattern needed is to match everything that is neither a letter nor a separator. Otherwise you would end up with "onetwothree" instead of the "one two three" you asked for.
[^\pL\pZ]
[^a-zA-Z] is a character class that matches every character apart from the letters a to z in lower or upper case.
The simplest form : [^a-z]
Could also be [^a-zA-Z] if you want to remove uppercase letters also.

Categories

Resources