0
votes

Not too familiar with regex, but I have a block of code that does not seem to be working as expected, I think I know why, but would be looking for a solution.

Here is the string "whereClause"

where filter_2_id = 20 and acceptable_flag is true

String whereClause = report.getWhereClause();
        String[] tokens = whereClause.split("filter_1_id");
        Pattern p = Pattern.compile("(\\d{3})\\d+");
        Matcher m = p.matcher(tokens[0]);
        List<Integer> filterList = new ArrayList<Integer>();
        if (m.find()) {
            do {
                String local = m.group();
                filterList.add(Integer.parseInt(local));
            } while (m.find());
        }

When I am debugging, it looks like it gets to the if (m.find()){ but then it just completely skips over it. Is it because the regex pattern (\d{3}\d+) only looks for numbers greater than 3 digits? I actually need it to scan for any set of numbers, so should i just include it as 0-9 inside?

Help/advice please

1
however if you look at the string, there is a number in there... i do not need that one, just any numbers after an = sign - Buccaneer
You haven't provided sample input and expected output - anubhava
\d will match 1 digit. It is the same as [0-9]. The expression {3} means to match the preceding pattern exactly 3 times. - dsh
I think you should split by "filter_2_id" instead of "filter_1_id". And then you should apply the pattern to tokens[1] instead of token[0]. And perhaps you should strip of the and clause of tokens[1] ... - CoronA

1 Answers

0
votes

You can try the regular expression "=\\s*(\\d+)" and then modify m.group() to m.group(1). This should look for an equal sign, possibly followed by some whitespace, and then a sequence of one or more digits. Putting the digits part in parentheses creates a group, which will be group 1 (group 0 is the whole match).