How can I escape individual regex metacharacters in Java?
For an Android app, I am working with files that contain many characters that regexes consider to have a special meaning. These include \?.()[*\^+'
and -
. I will be reading in two files:
- A dictionary list of words, each on a separate line.
- A list of characters that can be used to filter the words in the dictionary list.
A sample of each follows.
Dictionary:
/it*
t1*]
?\<t
item
(Yes, these are words. The first three are the contracted Braille ASCII representations of the words "stitch", "teacher", and "thought". Now you know.)
"Letters" to use:
?]*/\<1eitm
I want to include these letters in a regular expression similar to this:
String letters = "?]*/\<1eitm";
Pattern pattern = Pattern.compile("^["+letters+"]{4}$", Pattern.MULTILINE);
My aim is to select all the words from the dictionary list that include only the given characters and are the given length. I cannot control the order in which the requested characters will appear in the file.
If I use only non-metacharacters, like <1eitm
, this works fine. Somehow, I need to escape the metacharacters and ensure that characters such as ]
and -
appear in the right place inside the square brackets.
I could do this manually...but am hoping that there is a built-in command to do this for me. All I have found so far is the Pattern.quote()
command, which does not give me the results I want.
Below is a list of all the characters that I may need to use inside the square brackets:
\_-,;:!?.'"()[]@*/\&#%^+<=>~$0123456789abcdefghijklmnopqrstuvwxyz
And here is the barebones code that I am using for my Android test:
package com.example.quote;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AssetManager am = this.getAssets();
try {
String dictionary = readFile(am, "dictionary.txt");
String regex = readFile(am, "regex.txt");
regex = "^["+regex+"]{4}$"; // THIS IS WHERE I NEED TO MAKE A CHANGE
Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
Matcher matcher = pattern.matcher(dictionary);
while (matcher.find()) {
Log.d("TEST", matcher.group(0));
}
} catch (IOException e) {
e.printStackTrace();
}
}
private String readFile(AssetManager am, String fileName) throws IOException {
InputStream is = am.open(fileName);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String string = new String(buffer, "UTF-8");
return string;
}
}