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.)
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:
[$&+,:;=?@#|'<>.^*()%!-]
Regex for all special characters
regex - Java Regular expression for special characters across all languages - Stack Overflow
Regex to find special characters in Java - Stack Overflow
Java Regex to match a whole word with any combinationof special character - Stack Overflow
- 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.
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)
Try this.
Pattern regex = Pattern.compile("[$&+,:;=?@#|]");
Matcher matcher = regex.matcher("123=456");
if (matcher.find()){
// Do something
}
EDIT: matches() checks all the string and find() finds it in any part of the string.
A link: http://docs.oracle.com/javase/tutorial/essential/regex/index.html
Use String.matches(). Read the Javadocs for supported syntax.
Quote the target, but to prevent matches when the target is part of a larger word (eg event in the text prevent), add a word boundary if the end of the target is a word character:
str.matches(".*(?=\\b\\w|\\W)"
+ Pattern.quote(myString)
+ "(?<=\\w\\b|\\W).*")
Based on what I understood of your requirement, this should work:
String fixedString = "hello";
String stringToMatch = "{{hello}}";
String pattern = "\\W*"+Pattern.quote(fixedString)+"\\W*";
System.out.println("Match ?" + stringToMatch.matches(pattern));
It surrounds the fixed string with a non-word character repeat (0 or more times).
Case 2 seems the opposite as case 3, so I don't think you can combine the Patterns.
For case 2, your Pattern could look like:
Pattern pattern = Pattern.compile("(\\s|^)Hello(\\s|$)", Pattern.CASE_INSENSITIVE);
In this case we surround the keyword by whitespace or beginning/end of input.
For case 3, your Pattern could look like:
Pattern pattern = Pattern.compile("[\\$#@\\^&]Hello(\\s|$)", Pattern.CASE_INSENSITIVE);
In this case, we precede the keyword with any of the special characters of your choice (note the escaped reserved characters $ and ^), then we accept whitespace or the end of input as the character following the keyword.
Use (?:^|\s) ("start of text or whitespace") instead of the first \b, and (?:$|\s) ("end of text or whitespace") instead of the second \b in your regex.