[Java] 簡化Regex的使用,javaRegex

來源:互聯網
上載者:User

[Java] 簡化Regex的使用,javaRegex

 

使用

RegexString.with(string).pattern(pattern).start() + 後續操作(matches,find或者是replace)

 

源碼

package com;import java.util.Objects;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * @author YouXianMing1987@iCloud.com 用於簡化處理Regex */public class RegexString {    private String string;    private Pattern pattern;    private Matcher matcher;    ////////////////////// Constructor //////////////////////    /**     * Regex對象     *      * @param str     *            初始化用的字串     */    public RegexString(String str) {        setString(Objects.requireNonNull(str));    }    ////////////////////// Normal Method //////////////////////    /**     * 設定Regex的pattern     *      * @param regex     *            Regex語句     * @return RegexString     */    public RegexString pattern(String regex) {        setPattern(Pattern.compile(regex));        return this;    }    /**     * 設定Regex的pattern     *      * @param regex     *            Regex語句     * @param flags     *            Regexflag值     * @return RegexString     */    public RegexString pattern(String regex, int flags) {        setPattern(Pattern.compile(regex, flags));        return this;    }    /**     * Regex對象開始匹配(設定完pattern後需要自行此語句才能做後續操作)     *      * @return RegexString     */    public RegexString start() {        setMatcher(pattern.matcher(string));        return this;    }    /**     * 進行文本替換     *      * @param replacement     *            用來替換的文本     * @return 替換後的字串     */    public String replace(String replacement) {        return getMatcher().replaceAll(replacement);    }    /**     * 判斷是否匹配(一次性匹配全部文本,不分步)     *      * @return 匹配了返回true,沒有匹配返回false.     */    public boolean matches() {        return getMatcher().matches();    }    /**     * 判斷是否匹配(分步匹配文本,請結合while迴圈使用)     *      * @return 找到了返回true,沒有找到返回false.     */    public boolean find() {        return getMatcher().find();    }    /**     * find()操作成功後,可以通過matchString()擷取匹配的字串     *      * @return 匹配的字串     */    public String matchString() {        return getMatcher().group();    }    /**     * find()操作成功後,可以通過matchStart()擷取匹配的起始位置     *      * @return 匹配的起始位置     */    public int matchStart() {        return getMatcher().start();    }    /**     * find()操作成功後,可以通過matchEnd()擷取匹配的結束位置     *      * @return 匹配的起始位置     */    public int matchEnd() {        return getMatcher().end();    }    ////////////////////// Static Method //////////////////////    /**     * [靜態方法] 便利構造器     *      * @param str     *            初始化用的字串     * @return RegexString     */    public static RegexString with(String str) {        return new RegexString(str);    }    ////////////////////// Getter & Setter //////////////////////    public String getString() {        return string;    }    public void setString(String string) {        this.string = string;    }    public Pattern getPattern() {        return pattern;    }    public void setPattern(Pattern pattern) {        this.pattern = pattern;    }    public Matcher getMatcher() {        return matcher;    }    public void setMatcher(Matcher matcher) {        this.matcher = matcher;    }}

 

 

樣本

package com;public class Main {    public static void main(String args[]) {        // 尋找文本        {            String src = "This is my small example string which I'm going to use for pattern matching.";            RegexString string = RegexString.with(src).pattern("\\w+").start();            while (string.find()) {                System.out.println(string.matchStart() + "," + string.matchEnd() + " : " + string.matchString());            }        }                // 匹配        {            String src = "This is my small example string which I'm going to use for pattern matching.";            if (RegexString.with(src).pattern("^This.+$").start().matches()) {                System.out.println("Yes");            }        }        // 替換文本        {            String src = "This is my small example string which I'm going to use for pattern matching.";            System.out.println(RegexString.with(src).pattern("\\w+").start().replace("Regex"));        }        // 去掉字串首尾的空格,以及字串中間多餘的字串        {            String src = "   This    is    my small example string    which  I'm    going to    use  for  pattern  matching.     ";            String tmp = RegexString.with(src).pattern("^\\s+|\\s+$").start().replace("");            String des = RegexString.with(tmp).pattern("\\s+").start().replace(" ");            System.out.println("\"" + des + "\"");        }    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.