简体   繁体   中英

regex pattern matcher

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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM