How to force one or more metacharacters to be treated as ordinary characters in a Java Regular Expression (RegEx)
Posted: | Categories: Java, Programming | Tags: Metacharacter, RegExp
When using RegEx in Java you might face the need of treating one or more metacharacters as ordinary characters. As a reminder the metacharacters in a Java RegEx are:
([{^$|)?*+.
If you want to treat them as ordinary characters you have two options:
- Escape the metacharacter with a backslash,
- Enclose the whole string that contains metacharacters within
Q
andE
Q
means: “quotes all characters until E”, while E
ends the quotes.