make a string digestible for regexp - Android java.util.regex

Android examples for java.util.regex:Character Pattern

Description

make a string digestible for regexp

Demo Code

import android.content.Context;
import android.content.res.AssetManager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main{

    /**/*from   w  ww .  ja va2 s  .  co m*/
     * make a string digestible for regexp.
     * @param s
     * @return string with all non alphanumeric characters replaced with .*
     */
    public static String regexDigest(String s) {
        StringBuffer sb = new StringBuffer(s.length());
        for (int ich = 0; ich < s.length(); ich++) {
            char ch = s.charAt(ich);
            if (Character.isLetterOrDigit(ch)) {
                sb.append(ch);
            } else {
                sb.append(".*");
            }
        }
        return sb.toString();
    }

}

Related Tutorials