Trying to write a regular expression to check if the sentence as metacharacters "I need to make payment of $50 for the purchase, should i use CASH|CC". In this sentence i need to identify if metacharacters are present.
\\\\$
or ^(\\\\$)\\$
. What is the right syntax for Pattern.matches("^([\\\\$]$)", text);
to identify the special characters. I don't need to replace just identify if the sentence contains these characters.

- 1,323
- 1
- 15
- 18
-
@user85421, String.contains cannot be used, need to use set of metacharacters to filter. `String text = "Cash$50"; System.out.println(text.matches("\\\\$")); boolean match = Pattern.matches("\\\\$", text); System.out.println(match);` Both print false – tinker_fairy Feb 03 '20 at 07:18
-
2filter? but you asked only to identify (find)?? do not use `matches()` if you want to `find()` (or it must be `".*\\$.*"` to match any number of characters, one dollar and any number of characters) and only 2 backslashes needed – user85421 Feb 03 '20 at 07:20
-
example: `Pattern.compile("\\$").matcher("Cash $50").find()` or `Pattern.matches(".*\\$.*", "Cash $50")` – user85421 Feb 03 '20 at 07:26
2 Answers
If you want to know whether a string contains meta characters, you can use some like this:
boolean hasIt = sentence.chars().anyMatch(c -> "\\.[]{}()*+?^$|".indexOf(c) >= 0);
By not using the Regex engine, you don’t need to quote the characters which have a special meaning to it.
Using Pattern.matches
creates three unnecessary obstacles to the task. First, you have to quote all characters correctly, then, you need a regex construct to turn the characters into alternatives, e.g. [abc]
or a|b|c
, third, matches
checks whether the entire string matches the pattern, rather than contains an occurrences, so you’d need something like .*pattern.*
to make matches
to behave like find
, if you insist on it.
Which leads to the xy-problem of this task. It’s not clear which metacharacters you actually want to check and why you need this information in the first place.
If you want to search for this sentence within another text, just use Pattern.compile(sentence, Pattern.LITERAL)
to disable interpretation of meta characters. Or Pattern.quote(sentence)
when you want to assemble a pattern containing the sentence.
But if you don’t want to search for it, this information has no relevance. Note that “Is this a meta character?” may lead to a different answer than “Does it need quoting?”. Even this tutorial combines these questions in a misleading way. At two close places it names the metacharacters and describes the quoting syntax, leading to the wrong impression that all of these characters need quoting.
For example, -
only has a special meaning within a character class, so if there is no character class, which you detect by the presence of [
, the -
does not imply the presence of metacharacters. But while -
truly needs quoting within the character class, the characters =
and !
are metacharacters only in a certain context, which requires a metacharacter, so they never require quoting.
But if you are trying to check for a metacharacter to decide whether to use the Regex engine or to perform a plain text search, e.g. via String.indexOf
, you are performing premature optimization. This is not only a waste of development effort, optimizing before you even have an actual code you could measure often leads to the opposite result. Performing a pattern matching using the Regex engine with a string containing no metacharacters can lead to a more efficient search than a plain indexOf
on the String
. In the reference implementation, the Regex engine uses the Boyer Moore algorithm while the plaintext search methods on String
use a naive search.

- 285,553
- 42
- 434
- 765
Edit: As mentioned by commenters Andreas and Holger, the meta characters used by regular expressions are sometimes depending on a syntactical subdefinition, like character classes, specific sequences (lookahead, lookbehind,...) and are therefore not intrinsically metacaracters per se. Some are only meta characters in a specific context. However the answer provided here will include all possible meta characters, with the exception of the operators that only become meta characters when prefixed by \
. However, this means, that sometimes characters will be matched, in locations where they are not actually meta characters.
This question has half the answer: List of all special characters that need to be escaped in a regex
You can look at the javadoc of the Pattern class: http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html
The Java regular expression system exposes no character class for it's own special characters (regrettably).
Special constructs (named-capturing and non-capturing)
(?X) X, as a named-capturing group
(?:X) X, as a non-capturing group
(?idmsuxU-idmsuxU) Nothing, but turns match flags i d m s u x U on - off
(?idmsux-idmsux:X) X, as a non-capturing group with the given flags i d m s u x on - off
(?=X) X, via zero-width positive lookahead
(?!X) X, via zero-width negative lookahead
This block alone contains a lot (though not all) of the meta characters. The last two rows of the citation I had ot leave out, because the character sequences confused the parser of this page. I would suggest the following:
public static final Pattern META_CHARS = Pattern.compile("[\\\\\\]\\[(){}\\-!$?*+<>\\:\\.\\=\\,\\|^]");
But be aware, that this list might very well be incomplete, and that this contains typical characters such as ,
and .
which are part of the regex syntax. So you probably got a lot of escaping to do...
From there you can:
Matcher metaDetector = META_CHARS.matcher(stringToTest);
if (metaDetector.find()) {
// this is the found meta character...
String metaCharacter = metaDetector.group(0);
System.out.print(metaCharacter);
}
And if you want to find all meta characters, then make a while
out of if
in the above code snippet. If you do, for the line "I need to make \\payment{[ of $50 for !!the purc\"hase, sh###ould i use CASH|CC."
you receive \{[$!!,|.
, which is correct, as #
and "
are not meta characters in regex.
As Andreas correctly mentions, the exact pattern can be reduced to "[\\\\\\]\\[(){}^$?*+.|]"
, because this will tell you, whether or not at least one meta character is present. However this might miss some meta characters, if multiple are present. If this is not important, then the shorter chain is sufficient.

- 2,924
- 15
- 23
-
`"[\\\\\\]\\[(){}\\-!$?*+<>\\:\\.\\=\\,\\|]"` has too many characters, and unnecessary escapes. --- 1) These don't need escaping in a character class: `:.=,|` --- 2) These are only meta-characters when immediately after the `(?` special capture group prefix: `:=!<>` --- 3) This is only a meta-character in a character class: `-` --- 4) This is only a meta character in a `{}` quantifier: `,` --- 5) Missing: `^` --- *Conclusion:* It should be: `"[\\\\\\]\\[(){}^$?*+.|]"` – Andreas Feb 03 '20 at 08:33
-
@Andreas you are right. But if you go all that way, what about if two backslashes are present (i.o. words, if the meta character is already escaped)? I only printed a solution to find possible meta characters. It is not a guarantee that these are in fact unescaped meta characters. – TreffnonX Feb 03 '20 at 08:59
-
A plain text with *any* number of ``\`` backslashes is a text that would need escaping if used as a regex, which is the only time you would even care about regex meta characters. A meta character cannot be "already escaped" unless you treat the ``\`` as a meta character in the first place, and if you do that, then the text contains meta characters, which is what we're looking for. In short, it makes no difference if they are doubled in the text. – Andreas Feb 03 '20 at 09:05
-
Said another way: To the question "does the text ``foo\\bar`` contain any regex meta characters?", the answer is yes, because the first ``\`` is a regex meta character. – Andreas Feb 03 '20 at 09:07
-
True. Going with the OP's exact request, that would be correct. But then you would lose the information what meta characters are present. Event if you only need to know that meta characters are present, usually you want to erro log which one, so the user can handle the issue. – TreffnonX Feb 03 '20 at 09:11
-
Question says: *"I don't need to replace just **identify if the sentence contains these characters**."* OP is not interested in which ones are present. – Andreas Feb 03 '20 at 09:14
-
-
2The character `=` only has a special meaning when being preceded by `(?` or `(?<` which makes it as much a metacharacter as, e.g. `A`, as `A` has a special meaning when being preceded by `\\`. This makes the categorization of characters as “metacharacters” fuzzy, which might be the reason why the Regex engine doesn’t offer a “character class for it’s own special characters”; it would be unclear which characters should belong to this class. – Holger Feb 03 '20 at 11:21
-
@Holger Yes, this is also what Andreas mentioned. I designed my answer to be somewhat defensive about the characters involved, but of course, some of the characters are not intrinsically metacharacters, if apart from `\` any are... – TreffnonX Feb 03 '20 at 12:03
-
1The problem is, it’s impossible to pick the right characters without knowing why the OP actually needs this check. As I tried to explain in my answer, “*Is this a meta character?*” is a different question than “*Does it need quoting?*”. And that’s even a different question than “*Does it harm when I quote this?*”… – Holger Feb 03 '20 at 12:09