regex for java example


    public static String removeModelImpurities(String model)
    {
        Pattern regex = Pattern.compile("[^a-zA-Z0-9]$");
        String resultString = "";
        if (model!=null)
        {
            resultString=model.trim();
        }

        try {

            Matcher regexMatcher = regex.matcher(resultString);

            while(regexMatcher.find())
            {
                try {
                    regexMatcher = regex.matcher(resultString);
                    try {
                        resultString = regexMatcher.replaceAll("");
//                        System.out.println(resultString);
                    } catch (IllegalArgumentException ex) {
                        // Syntax error in the replacement text (unescaped $ signs?)
                    } catch (IndexOutOfBoundsException ex) {
                        // Non-existent backreference used the replacement text
                    }
                } catch (PatternSyntaxException ex) {
                    // Syntax error in the regular expression
                }
                regexMatcher = regex.matcher(resultString);
                if(resultString.length()==0)
                    break;
            }
        } catch (PatternSyntaxException ex) {
            // Syntax error in the regular expression
        }

        return resultString;
    }

原文地址:https://www.cnblogs.com/lexus/p/2382554.html