If there are no restrictions such as "must start with a letter" or "must contain at least one letter" (your question doesn't specify any), and assuming that by "white spaces" you mean a space, but not tabs, newlines, etc., then the expression would just be:
Pattern.compile("^[\\$#\\+{}:\\?\\.,~@\"a-zA-Z0-9 ]+$");
Note that this regular expression allows things such as only a single space, or a user name identical to another one except for the number of trailing spaces, etc. if you want to be a little more restrictive and enforce starting with a letter or number (for example), you could do:
Pattern.compile("^[a-zA-Z0-9][\\$#\\+{}:\\?\\.,~@\"a-zA-Z0-9 ]+$");
Answer from hair raisin on Stack OverflowIf there are no restrictions such as "must start with a letter" or "must contain at least one letter" (your question doesn't specify any), and assuming that by "white spaces" you mean a space, but not tabs, newlines, etc., then the expression would just be:
Pattern.compile("^[\\$#\\+{}:\\?\\.,~@\"a-zA-Z0-9 ]+$");
Note that this regular expression allows things such as only a single space, or a user name identical to another one except for the number of trailing spaces, etc. if you want to be a little more restrictive and enforce starting with a letter or number (for example), you could do:
Pattern.compile("^[a-zA-Z0-9][\\$#\\+{}:\\?\\.,~@\"a-zA-Z0-9 ]+$");
Use this pattern which allows characters that are not on your blacked listed characters.
"[^^;\\-()<>|='%_]+"
The first ^ means NOT any of the following characters. Until you clarify if the $ is good or bad, I'm treating it as a good character.
Code sample:
public static void main(String[] args) throws Exception {
List<String> userNames = new ArrayList() {{
add("Name1$"); // Good
add("Name1# Name2"); // Good
add("Name1 Name2"); // Good
add("Name Name2"); // Good
add("Name1 Name"); // Good
add("UserName$"); // Good
add("UserName^"); // Bad
add("UserName;"); // Bad
add("User-Name"); // Bad
add("User_Name"); // Bad
}};
Pattern pattern = Pattern.compile("[^^;\\-()<>|='%_]+");
for (String userName : userNames) {
if (pattern.matcher(userName).matches()) {
System.out.println("Good Username");
} else {
System.out.println("Bad Username");
}
}
}
Results:
Good Username
Good Username
Good Username
Good Username
Good Username
Good Username
Bad Username
Bad Username
Bad Username
Bad Username
java - Regex pattern including all special characters - Stack Overflow
java - Need a regular expression for field which should allow special characters, alphanumeric characters, and spaces - Stack Overflow
java - Regular expression include and exclude special characters - Stack Overflow
java - Android: Accept all special characters in regex - Stack Overflow
Please don't do that... little Unicode BABY ANGELs like this one 👼 are dying! ◕◡◕ (← these are not images) (nor is the arrow!)
☺
And you are killing 20 years of DOS :-) (the last smiley is called WHITE SMILING FACE... Now it's at 263A... But in ancient times it was ALT-1)
and his friend
☻
BLACK SMILING FACE... Now it's at 263B... But in ancient times it was ALT-2
Try a negative match:
Pattern regex = Pattern.compile("[^A-Za-z0-9]");
(this will ok only A-Z "standard" letters and "standard" 0-9 digits.)
You have a dash in the middle of the character class, which will mean a character range. Put the dash at the end of the class like so:
[$&+,:;=?@#|'<>.^*()%!-]
You simply need to escape the special characters. Try:
[a-zA-Z0-9\-#\.\(\)\/%&\s]{0,19}
You can test your regular expressions on http://rubular.com/
Your regex is incorrect in at least one way - if you're considering a hyphen to be a "special character", then you should put it at the beginning or end of the range. So: [a-zA-Z0-9#.()/%&\s-]{0,19}.
Characters that are "special" within the context of the regex itself are often not parsed if they're inside a range. So you're fine with ., ( and ). But check your parser to make sure that it understands what \s means. It might be simpler just to put a space.
Also, if your regex parser tends to delimit the regex with slashes, then you may have to escape the slash in the middle of the range: [a-zA-Z0-9#.()\/%&\s-]{0,19}.
For the allowed characters you can use
^[a-zA-Z0-9~@#$^*()_+=[\]{}|\\,.?: -]*$
to validate a complete string that should consist of only allowed characters. Note that - is at the end (because otherwise it'd be a range) and a few characters are escaped.
For the invalid characters you can use
[<>'"/;`%]
to check for them.
To combine both into a single regex you can use
^(?=[a-zA-Z0-9~@#$^*()_+=[\]{}|\\,.?: -]*$)(?!.*[<>'"/;`%])
but you'd need a regex engine that allows lookahead.
You haven't actually asked a question, but assuming you have one, this could be your answer...
Assuming all characters, except the "Special Characters" are allowed you can write
String regex = "^[^<>'\"/;`%]*$";
Is there anything you want to disallow? It sounds like you're trying to allow all alphanumeric character and allow all non-alphanumeric (i.e., special) characters.
The following regex will match all special characters:
[^A-Za-z0-9]
Instead of adding every special character, use .*.
This regex matches everything.
If you want to remove some characters from it, use [^a!c] alone, without .*, to match everything except a, ! and c.
would you say the below is ok to enter in one special character
Regexp('.*[\¬\!\"\£\$\%\^\&\*\(\)\_\+\`\-\=\{\}\:\@\~\<\>\?\[\]\;\'\#\,\.\/\\\|]'
Yes, your expression will match any string that includes at least one of the specified special characters
hi all,
would you say the below is ok to enter in one special character
Regexp('.*[\¬\!\"\£\$\%\^\&\*\(\)\_\+\`\-\=\{\}\:\@\~\<\>\?\[\]\;\'\#\,\.\/\\\|]'
thanks,
rob
You may use \\p{Alnum} to match any kind of letter or digit from any language.
private static final String ADDRESS_LINE_PATTERN = "[\\p{Alnum},\\s#\\-.]+";
See Pattern javadoc for more info.
Normally, in Unicode aware regex engines, the word class \w will
include all alpha-num's from any language.
You could use the negative of it \Win a negative class [^\W], add whatever
you don't want _, then OR it with special characters you do want.
(?U)(?:[^\W_]|[-,.+\s#])+
edit - re: @nhahtdh comment
Added Java (?U) inline modifier for UNICODE_CHARACTER_CLASS
(I assume correct)
This worked for me:
String result = str.replaceAll("[^\\dA-Za-z ]", "").replaceAll("\\s+", "+");
For this input string:
/-+!@#$%^&())";:[]{}\ |wetyk 678dfgh
It yielded this result:
+wetyk+678dfgh
replaceAll expects a regex:
public static final String specialChars2 = "[`~!@#$%^&*()_+[\\]\\\\;\',./{}|:\"<>?]";
- Java characters that have to be escaped in regular expressions are:
\.[]{}()<>*+-=!?^$| - Two of the closing brackets (
]and}) only have to be escaped after opening the same type of bracket. - In
[]-brackets some characters (like+and-) do sometimes work without escape.
You can look at the javadoc of the Pattern class: http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html
You need to escape any char listed there if you want the regular char and not the special meaning.
As a maybe simpler solution, you can put the template between \Q and \E - everything between them is considered as escaped.
You can use
^[a-zA-Z][a-zA-Z0-9]*(?:[ _-][a-zA-Z0-9]+)*$
See the regex demo
Details:
^- start of string[a-zA-Z][a-zA-Z0-9]*- a letter and then zero or more letters or digits(?:[ _-][a-zA-Z0-9]+)*- zero or more sequences of a space/_/-and then one or more letters or digits$- end of string.
Please try this regex.
^[a-zA-Z][A-Za-z0-9 ()/]*$
You can add whatever special character you want in your regex. Modify it as per your requirement.