正则表达式举例

最近工作中碰到了正则表达式,就稍微研究了下。真是处理复杂字符串问题的利器!

在此记录几个例子,方便日后查阅。

1、查找匹配的子串

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

// 查找在${}中,以$开头的字母串
public class MatchTest {
    public static void main(String[] args) {
        String inputLine = "<span style=\"color:rgb(255,153,0);font-size:13px;font-family:AdobeArabic-Regular;\">${=$a}</span><span style=\"color:rgb(255,153,0);font-size:13px;font-family:AdobeArabic-Regular;\">${=dj($abc)}</span>";
        Pattern r = Pattern.compile("\\$\\{=.*?(\\$\\w+)[\\W]");
        Matcher m = r.matcher(inputLine);
        while(m.find()) {
            System.out.println(m.group(1));
        }
    }
}

输出:

$a
$abc

2、匹配两个字段

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

public class REmatchTwoFields {
    public static void main(String[] args) {
        String inputLine = "Adams,  Jonh Quincy";
        Pattern r = Pattern.compile("(.*),  (.*)");
        Matcher m = r.matcher(inputLine);
        if (!m.matches())
            throw new IllegalArgumentException("Bad input");
        System.out.println(m.group(2) + ' ' + m.group(1));
    }
}

输出:

Jonh Quincy Adams

3、字符串分割

Java 中,String.split 方法是直接支持正则表达式的。下面的例子中,用被 <br> 包围的,3个及以上的"-"分割字符串。(即单独一行的,3个及以上的减号,与 markdown 语法中的分割线一致)。

public class SplitTest {
    public static void test() {
        String[] texts = new String[4];
        texts[0] = "aaa<br><span>haha</span><br><span class='dlsn' style='dsljasf' font='sdkb'>------</span><br>bbb<br><span>----</span>  <br>----<br>ccc---";
        texts[1] = "<span style=\"font-size:13px;font-family:AdobeArabic-Regular;\">${=formletName}</span><span style=\"font-size:13px;font-family:AdobeArabic-Regular;\"><br></span><span style=\"font-size:13px;font-family:AdobeArabic-Regular;\">----<br>729374${=formletName}</span>";
        texts[2] = "<span style=\"color:rgb(0,0,255);font-size:13px;font-family:Dialog;\"><strong>2017-04-28</strong></span><span style=\"color:rgb(0,0,255);font-size:13px;font-family:Dialog;\"><strong><br></strong></span><span style=\"color:rgb(0,0,255);font-size:13px;font-family:Dialog;\"><strong>----<br>2017-04-28 09:23:55</strong></span>";
        texts[3] = "<span style=\"color:rgb(255,0,0);font-size:15px;font-family:Dialog;\"><strong>${=sql(\"FRDemo\", \"SELECT CUSTOMERNAME FROM CUSTOMER where CUSTOMERID=1\",1)}</strong></span><span style=\"color:rgb(255,0,0);font-size:15px;font-family:Dialog;\"><strong><br></strong></span><span style=\"color:rgb(255,0,0);font-size:15px;font-family:AdobeArabic-Bold;\"><strong>----</strong></span><span style=\"color:rgb(255,0,0);font-size:15px;font-family:Dialog;\"><strong><br></strong></span><span style=\"color:rgb(255,0,0);font-size:15px;font-family:Dialog;\"><strong>${=IF(false,\"first\",\"second\")}</strong></span><span style=\"color:rgb(255,0,0);font-size:15px;font-family:Dialog;\"><strong><br></strong></span><span style=\"color:rgb(255,0,0);font-size:15px;font-family:AdobeArabic-Bold;\"><strong>----</strong></span><span style=\"color:rgb(255,0,0);font-size:15px;font-family:Dialog;\"><strong><br> </strong></span><span style=\"color:rgb(255,0,0);font-size:15px;font-family:Dialog;\"><strong>${=sum(1,1)}</strong></span>";
        for (int i = 0; i < texts.length; i++) {
            String[] ss = texts[i].split("<br>((\\s*?</?\\w[^>]*?>)*?-{3,}(</?\\w[^>]*?>)*?\\s*?<br>)+");
            for (String s: ss) {
                System.out.println(s);
            }
            System.out.println("\n\n");
        }
    }
    public static void main(String[] args) {
        test();
    }
}

输出:

aaa<br><span>haha</span>
bbb
ccc---



<span style="font-size:13px;font-family:AdobeArabic-Regular;">${=formletName}</span><span style="font-size:13px;font-family
:AdobeArabic-Regular;">
729374${=formletName}</span>



<span style="color:rgb(0,0,255);font-size:13px;font-family:Dialog;"><strong>2017-04-28</strong></span><span style="color:rg
b(0,0,255);font-size:13px;font-family:Dialog;"><strong>
2017-04-28 09:23:55</strong></span>



<span style="color:rgb(255,0,0);font-size:15px;font-family:Dialog;"><strong>${=sql("FRDemo", "SELECT CUSTOMERNAME FROM CUST
OMER where CUSTOMERID=1",1)}</strong></span><span style="color:rgb(255,0,0);font-size:15px;font-family:Dialog;"><strong>
</strong></span><span style="color:rgb(255,0,0);font-size:15px;font-family:Dialog;"><strong>${=IF(false,"first","second")}<
/strong></span><span style="color:rgb(255,0,0);font-size:15px;font-family:Dialog;"><strong>
 </strong></span><span style="color:rgb(255,0,0);font-size:15px;font-family:Dialog;"><strong>${=sum(1,1)}</strong></span>

4、JS 字符串分割

js 字符串的 split 方法也支持正则表达式,不过语法有点变化。

> var value = "你好啊,<br>----------<br>李银河";
> value.split(/<br>-{3,}<br>/);  // ["你好啊,", "李银河"]

 

附:正则表达式在线测试及常用正则表达式参考