- 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.
- 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.
Regex for special characters in java - Stack Overflow
java - Regex pattern including all special characters - Stack Overflow
android - Check for special characters in a string using regex in java - Stack Overflow
regex - find out for special characters in Java - Stack Overflow
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 = "[`~!@#$%^&*()_+[\\]\\\\;\',./{}|:\"<>?]";
You can use Pattern matcher for check special character and you can check below example:
Pattern regex = Pattern.compile("[$&+,:;=\\\\?@#|/'<>.^*()%!-]");
if (regex.matcher(your_string).find()) {
Log.d("TTT, "SPECIAL CHARS FOUND");
return;
}
Hope this helps you...if you need any help you can ask
An easy way is to check if a string has any non-alphanumeric characters.
TRY THIS,
StringChecker.java
public class StringChecker {
public static void main(String[] args) {
String str = "abc$def^ghi#jkl";
Pattern p = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(str);
System.out.println(str);
int count = 0;
while (m.find()) {
count = count+1;
System.out.println("position " + m.start() + ": " + str.charAt(m.start()));
}
System.out.println("There are " + count + " special characters");
}
}
And you get the result look like below:
$ java SpecialChars
abc$def^ghi#jkl
position 3: $
position 7: ^
position 11: #
There are 3 special characters
You can pass your own patterns as param in compile methods as per your needs to checking special characters:
Pattern.compile("[$&+,:;=\\\\?@#|/'<>.^*()%!-]");
Instead of matching many special characters. Just check if word has any non-alphanumeric character. Like this:
String term = "Hello-World";
Pattern p = Pattern.Compile(".*\\W+.*");
Matcher m = p.Matcher(term);
Or you can also use:
String term = "Hello-World";
Pattern p = Pattern.Compile("[^A-Za-z0-9]");
Matcher m = p.Matcher(term);
Just list the characters inside a character class ([...]), escaping as necessary. Since this sounds like a school assignment, I'm not going to spoon-feed it to you. Note that you may need to double-escape some characters. (The regex requires \ to be escaped as \\, but to get \\ into a regex in Java, you need to use "\\\\".)
Incidentally, you seem to have a couple of characters ([ and ]) in your list twice.
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.
I wrote this pattern:
Pattern SPECIAL_REGEX_CHARS = Pattern.compile("[{}()\\[\\].+*?^$\\\\|]");
And use it in this method:
String escapeSpecialRegexChars(String str) {
return SPECIAL_REGEX_CHARS.matcher(str).replaceAll("\\\\$0");
}
Then you can use it like this, for example:
Pattern toSafePattern(String text)
{
return Pattern.compile(".*" + escapeSpecialRegexChars(text) + ".*");
}
We needed to do that because, after escaping, we add some regex expressions. If not, you can simply use \Q and \E:
Pattern toSafePattern(String text)
{
return Pattern.compile(".*\\Q" + text + "\\E.*")
}
Is there any method in Java or any open source library for escaping (not quoting) a special character (meta-character), in order to use it as a regular expression?
If you are looking for a way to create constants that you can use in your regex patterns, then just prepending them with "\\" should work but there is no nice Pattern.escape('.') function to help with this.
So if you are trying to match "\\d" (the string \d instead of a decimal character) then you would do:
// this will match on \d as opposed to a decimal character
String matchBackslashD = "\\\\d";
// as opposed to
String matchDecimalDigit = "\\d";
The 4 slashes in the Java string turn into 2 slashes in the regex pattern. 2 backslashes in a regex pattern matches the backslash itself. Prepending any special character with backslash turns it into a normal character instead of a special one.
matchPeriod = "\\.";
matchPlus = "\\+";
matchParens = "\\(\\)";
...
In your post you use the Pattern.quote(string) method. This method wraps your pattern between "\\Q" and "\\E" so you can match a string even if it happens to have a special regex character in it (+, ., \\d, etc.)