Java正则表达式提示错误字符

时间:2016-11-24 03:57:15

标签: java regex

通常,正则表达式只能指示语句和规则是否匹配。我希望能够在我不知道时指出错误的角色,我该怎么做。

Pattern compile = Pattern.compile("[a-zA-Z]*");
Matcher matcher = compile.matcher("abc1ac");

我想在角色1中犯一个错误。

我想检查过滤器,例如(name eq '1' or nickname ne '2') and code like '''' and user.name like 'jo%',使用double''翻译

2 个答案:

答案 0 :(得分:1)

I write as follow,maybe better to use design pattern.

    public class FilterExpressionException extends RuntimeException {

        private String expression;
        private int index;

        public FilterExpressionException(String expression, int index) {
            super(mark(expression, index));
            this.expression = expression;
            this.index = index;
        }

        public FilterExpressionException(String message, String expression, int index) {
            super(message + "(" + mark(expression, index) + ")");
            this.expression = expression;
            this.index = index;
        }

        private static String mark(String expression, int index) {
            return expression.substring(0, index) + '^' + expression.substring(index);
        }

        public String getExpression() {
            return expression;
        }

        public int getIndex() {
            return index;
        }

    }

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Stack;

    public class FilterExpressionParserImpl {


        private String[] relations = {"eq", "ne", "gt", "ge", "lt", "le", "like", "in", "bt"};

        private String[] logices = {"and", "or"};

        private char[][] relationChars = toCharArray(relations);

        private char[][] logicChars = toCharArray(logices);

        public static void main(String[] args) {
            FilterExpressionParserImpl impl = new FilterExpressionParserImpl();
            String expr = "(name eq '' or  nickname  ne  '2' )  and (code like '''' or (user.A.name eq '''go''go''go'))";
            List<String> parse = impl.parse(expr);
            System.out.println(parse);
            List<String> strings = impl.followUp(parse);
            System.out.println(strings);
        }


        private class Variable {

            Stack<String> words = new Stack<>();

            Stack<Character> characters = new Stack<>();

            Stack<Integer> brackets = new Stack<>();

            int step = 0;

            char character;

            int commaTimes = 0;

            char[][] relations;

            char[][] logices;

            public Variable(char[][] relations, char[][] logices) {
                this.relations = relations;
                this.logices = logices;
            }
        }

        private List<String> followUp(List<String> middles) {
            List<String> afters = new ArrayList<>(middles.size());
            Stack<String> operators = new Stack<>();
            String top;
            for (String middle : middles) {
                switch (middle) {
                    case "and":
                    case "or":
                        if (operators.size() > 0 && !operators.peek().equals("(")) afters.add(operators.pop());
                        operators.push(middle);
                        break;
                    case "(":
                        operators.push(middle);
                        break;
                    case ")":
                        while (!(top = operators.pop()).equals("(")) afters.add(top);
                        break;
                    default:
                        afters.add(middle);
                }
            }
            while (!operators.isEmpty()) afters.add(operators.pop());
            return afters;
        }

        private List<String> parse(String filter) {
            filter = filter.trim();

            Variable variable = new Variable(relationChars, logicChars);
            for (int i = 0; i < filter.length(); i++) {
                variable.character = filter.charAt(i);
                switch (variable.character) {
                    case ' ':

                        if (variable.characters.isEmpty()) continue;
                        switch (variable.step) {

                            case 0:
                                dealPropertyPathEnd(filter, i, variable);
                                break;

                            case 1:
                                dealRelationEnd(variable);
                                break;

                            case 2:
                                dealValueEnd(filter, i, variable);
                                break;

                            case 3:
                                dealLogicEnd(variable);
                                break;
                        }

                        pushWord(variable);
                        break;
                    case '(':

                        if (variable.step != 0) throw new FilterExpressionException(filter, i);
                        variable.words.push(String.valueOf(variable.character));
                        variable.brackets.push(i);
                        break;
                    case ')':

                        if (variable.brackets.size() == 0) throw new FilterExpressionException(filter, i);
                        variable.brackets.pop();

                        if (variable.step == 2 && !variable.characters.isEmpty()) {
                            dealValueEnd(filter, i, variable);
                            pushWord(variable);
                        }

                        if (variable.step != 3) throw new FilterExpressionException(filter, i);

                        variable.words.push(String.valueOf(variable.character));
                        break;
                    default:
                        switch (variable.step) {

                            case 0:

                                if (!(isLetter(variable.character) || variable.character == '.')
                                        || ((variable.characters.size() == 0 || variable.characters.peek() == '.') && variable.character == '.'))
                                    throw new FilterExpressionException(filter, i);
                                break;

                            case 1:

                                variable.relations = find(variable.relations, variable.characters.size(), variable.character);
                                if (variable.relations == null) throw new FilterExpressionException(filter, i);
                                break;

                            case 2:

                                if (variable.characters.size() == 0) {
                                    if (variable.character != '\'') throw new FilterExpressionException(filter, i);
                                }

                                else {
                                    if (variable.character == '\'') variable.commaTimes++;
                                    else if (variable.commaTimes % 2 != 0) throw new FilterExpressionException(filter, i);
                                }
                                break;

                            case 3:

                                variable.logices = find(variable.logices, variable.characters.size(), variable.character);
                                if (variable.logices == null) throw new FilterExpressionException(filter, i);
                                break;
                        }
                        variable.characters.push(variable.character);
                        break;
                }
            }

            if (!variable.characters.isEmpty()) {

                if (variable.characters.peek() != '\'') throw new FilterExpressionException(filter, filter.length() - 1);

                if (variable.commaTimes % 2 != 1) throw new FilterExpressionException(filter, filter.length() - 1 - variable.commaTimes);

                pushWord(variable);
            }

            if (!variable.brackets.isEmpty()) throw new FilterExpressionException(filter, variable.brackets.firstElement());

            return variable.words;
        }

        private void pushWord(Variable variable) {
            String string = join(variable.characters);
            if (variable.step == 3) {
                if (string.equals("\'\'")) {
                    string = "";
                } else {
                    string = string.replace("\'\'", "\'");
                    string = string.substring(1, string.length() - 1);
                }
            }

            variable.words.push(string);
            variable.characters.clear();
        }

        private String join(List<Character> characters) {
            StringBuilder builder = new StringBuilder();
            characters.forEach(builder::append);
            return builder.toString();
        }


        private void dealPropertyPathEnd(String filter, int i, Variable variable) {

            if (variable.characters.peek() == '.') throw new FilterExpressionException(filter, i);
            variable.step = 1;
        }

        private void dealRelationEnd(Variable variable) {
            variable.relations = relationChars;
            variable.step = 2;
        }

        private void dealValueEnd(String filter, int i, Variable variable) {

            if (variable.characters.peek() != '\'') throw new FilterExpressionException(filter, i);

            if (variable.commaTimes % 2 != 1) throw new FilterExpressionException(filter, i);
            variable.commaTimes = 0;
            variable.step = 3;
        }

        private void dealLogicEnd(Variable variable) {
            variable.logices = logicChars;
            variable.step = 0;
        }


        private boolean isLetter(char character) {
            return ('a' <= character && character <= 'z') || ('A' <= character && character <= 'Z');
        }


        private char[][] toCharArray(String[] strings) {
            char[][] chars = new char[strings.length][];
            for (int i = 0; i < strings.length; i++) {
                chars[i] = strings[i].toCharArray();
            }
            return chars;
        }


        private char[][] find(char[][] sources, int column, char character) {
            if (sources == null || sources.length == 0) return sources;

            List<Integer> indexes = new ArrayList<>(sources.length);
            for (int i = 0; i < sources.length; i++) {
                if (sources[i].length > column && sources[i][column] == character) indexes.add(i);
            }

            if (indexes.isEmpty()) return null;

            char[][] targets = new char[indexes.size()][];
            for (int i = 0; i < indexes.size(); i++) {
                targets[i] = sources[indexes.get(i)];
            }
            return targets;
        }


    }

答案 1 :(得分:0)

Java的正则表达式引擎可以做的不仅仅是匹配文本。有关详细信息,请read the documentation

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LookingAt {

    public static String validate(String input, Pattern regex) {
        Matcher m = regex.matcher(input);
        if (m.matches())
            return "OK";
        if (m.lookingAt())
            return "First error at index " + m.end();
        if (m.hitEnd())
            return "Too short";
        return "Cannot match at all";
    }

    public static void main(String[] args) {
        System.out.println(validate("abcac", Pattern.compile("[a-zA-Z]*")));
        System.out.println(validate("abc1ac", Pattern.compile("[a-zA-Z]*")));
        System.out.println(validate("abcac", Pattern.compile("[a-zA-Z]{10,}")));
        System.out.println(validate("1abcac", Pattern.compile("[a-zA-Z]+")));
    }
}