正则:零宽断言和负向零宽断言

来源:互联网 发布:淘宝包包 编辑:程序博客网 时间:2024/05/06 02:31

http://deerchao.net/tutorials/regex/regex.htm#lookaround

这里写图片描述

这里写图片描述

这里写图片描述

代码展现

package test;import java.util.regex.Matcher;import java.util.regex.Pattern;public class RegexUtil {    public static void main(String[] args) {        /***         * .*(?=>)    600         *          * (?<=>=)+.*(?=and)  2         *          * (?<=\\()+.*(?=!=)  int         * 其中:\\为转义字符         * (?<=!=)+.*(?=or)  int         *          * (?<=or)+.*(?=<=)  2         *          * (?<=<=)+.*(?=\\))  600         */    String input = "600>=2 and (int != int or 2 <= 600)";    String regex = "(?<=<=)+.*(?=\\))";    System.out.println(Match(input , regex));    }    /**     *      * 正则表达式辅助类     *      * @param input 字符串     * @param regex 正则表达式     * @return 正则表达式匹配结果     */public static String Match(String input, String regex) {    Pattern praiseCompile = Pattern.compile(regex);    Matcher praiseMatcher = praiseCompile.matcher(input);    if (praiseMatcher.find()) {        return praiseMatcher.group(0).trim();    }    return null;    }}
0 0
原创粉丝点击