正则表达式:(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))$

JAV编程思想学习过程中遇到该问题,刚开始不太明白这个正则表达式是如何匹配的,后来慢慢理解了,现与大家分享我的理解思路。

static public final String POEM = 
            "Twas brilling, and the slithy toves\n" +
            "Did gyre and gimble in the wabe.\n" +
            "All mimsy were the borogoves.\n" +
            "And the more raths outgrabe.\n\n" +
            "Beware the Jabberwock, my son,\n" +
            "The jaws that bite, the claws that catch.\n" +
            "The frumious Bandersnatch.";
public static void main(String[] args) {
        // TODO Auto-generated method stub

        Matcher m = Pattern.
        compile("(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))$").matcher(POEM);
        while(m.find()){
            for(int j = 0; j <= m.groupCount(); j++)
                System.out.print("[" + m.group(j) + "]");
            System.out.println();
        }
    }

先分析这个表达式:
1)(?m)表示按照多行模式每次提取一行进行匹配;
2)\S+表示匹配一个或多个非空白符;
3)\s+表示匹配一个或多个空白符(包括空格、tab、换行、换页和回车);
4)$表示从靠近行尾的地方取数据,表示一行的末尾。

(?m)(\S+)\s+((\S+)\s+(\S+))$:取靠近行尾的数据形式:(数据)空白字符((数据)空白字符(数据))结尾
意即捕获每行的最后3个词。

一行即m中的一个匹配,m.groupCount()返回某一行匹配模式中的分组数目,第0组即匹配的整个表达式。有如下表达式:A(B(C))D
组0是ABCD,组1是BC,组2是C

m.group(j)根据前一次匹配操作期间得到的组号j返回与该组号对应的字符串。

下面是上述代码的输出结果:

[the slithy toves][the][slithy toves][slithy][toves]
[in the wabe.][in][the wabe.][the][wabe.]
[were the borogoves.][were][the borogoves.][the][borogoves.]
[more raths outgrabe.][more][raths outgrabe.][raths][outgrabe.]
[Jabberwock, my son,][Jabberwock,][my son,][my][son,]
[claws that catch.][claws][that catch.][that][catch.]
[The frumious Bandersnatch.][The][frumious Bandersnatch.][frumious][Bandersnatch.]

以第一行为例,结果输出了最后三个单词。由正则表达式可知每一行匹配的分组数目是5个,组0是[the slithy toves][the][slithy toves][slithy][toves];组1是[the];组2是[slithy toves];组3是[slithy];组4是[toves]。
后面依此类推。

下面是不加行尾标识符$的例子:

Matcher m = Pattern.
        compile("(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))").matcher(POEM);
        while(m.find()){
            for(int j = 0; j <= m.groupCount(); j++)
                System.out.print("[" + m.group(j) + "]");
            System.out.println();
        }

输出结果:

[Twas brilling, and][Twas][brilling, and][brilling,][and]
[the slithy toves][the][slithy toves][slithy][toves]
[Did gyre and][Did][gyre and][gyre][and]
[gimble in the][gimble][in the][in][the]
[wabe.
All mimsy][wabe.][All mimsy][All][mimsy]
[were the borogoves.][were][the borogoves.][the][borogoves.]
[And the more][And][the more][the][more]
[raths outgrabe.

Beware][raths][outgrabe.

Beware][outgrabe.][Beware]
[the Jabberwock, my][the][Jabberwock, my][Jabberwock,][my]
[son,
The jaws][son,][The jaws][The][jaws]
[that bite, the][that][bite, the][bite,][the]
[claws that catch.][claws][that catch.][that][catch.]
[The frumious Bandersnatch.][The][frumious Bandersnatch.][frumious][Bandersnatch.]

可看出如果在正则表达式末尾不加行尾标识符$,则字符串中的每3个词将会被捕获一次。换行符\n将会被当做空白符\s+匹配。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值