亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

模式匹配器返回意外值

模式匹配器返回意外值

回首憶惘然 2023-09-13 10:13:54
你能看一下下面結果中的模式(java)嗎?我預計結果應該給我 3 個組值,但相反它給了我 2 個值,只是我認為我錯過了一些東西。package com.mycompany.testapp;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * * @author bo17a */public class NewClass {    public static void main(String[] args){        Pattern mPattern = Pattern.compile("^love (.*?) way you (.*?)$");        Matcher matcher = mPattern.matcher("love the way you lie");        if(matcher.find()){            String[] match_groups = new String[matcher.groupCount()];            System.out.println(String.format("groupCount: %d", matcher.groupCount()));            for(int j = 0;j<matcher.groupCount();j++){                System.out.println(String.format("j %d",j));                match_groups[j] = matcher.group(j);                System.out.println(match_groups[j]);            }        }    }}我得到的結果是:2love the way you liethe但我的預期結果應該是:3love the way you liethelie更新我嘗試按照回復中的建議添加一組號碼:/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package com.mycompany.testapp;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * * @author lee */public class NewClass {    public static void main(String[] args){        Pattern mPattern = Pattern.compile("^love (.*?) way you (.*?)$");        Matcher matcher = mPattern.matcher("love the way you lie");        if(matcher.find()){            String[] match_groups = new String[matcher.groupCount()];                System.out.println(String.format("groupCount: %d", matcher.groupCount()));                for(int j = 0;j<=matcher.groupCount();j++){                    System.out.println(String.format("j %d",j));                    match_groups[j] = matcher.group(j);                    System.out.println(match_groups[j]);                }        }    }}我已經在 Windows 10 JDK 8 Mac OS JDK 8 上嘗試過。那么,這可能是 Java 中的一個錯誤,因為你和我對相同的代碼有不同的結果?
查看完整描述

2 回答

?
狐的傳說

TA貢獻1804條經驗 獲得超3個贊

我的猜測是,您還希望以團隊形式捕捉完整比賽,


^(love (.*?) way you (.*?))$

或者直接在你的計數器上加 1:


測試1

import java.util.regex.Matcher;

import java.util.regex.Pattern;



public class RegularExpression{


    public static void main(String[] args){


        Pattern mPattern = Pattern.compile("^love (.*?) way you (.*?)$");

        Matcher matcher = mPattern.matcher("love the way you lie");

        if(matcher.find()){

            String[] match_groups = new String[matcher.groupCount() + 1];

                System.out.println(String.format("groupCount: %d", matcher.groupCount() + 1));

                for(int j = 0;j<matcher.groupCount() + 1;j++){

                    System.out.println(String.format("j %d",j));

                    match_groups[j] = matcher.group(j);

                    System.out.println(match_groups[j]);

                }

        }


    }

}

輸出

groupCount: 3

j 0

love the way you lie

j 1

the

j 2

lie

測試2

import java.util.regex.Matcher;

import java.util.regex.Pattern;



public class RegularExpression{


    public static void main(String[] args){


        final String regex = "^(love (.*?) way you (.*?))$";

        final String string = "love the way you lie";


        final Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);

        final Matcher matcher = pattern.matcher(string);


        while (matcher.find()) {

            System.out.println("Full match: " + matcher.group(0));

            for (int i = 1; i <= matcher.groupCount(); i++) {

                System.out.println("Group " + i + ": " + matcher.group(i));

            }

        }


    }

}

輸出

Full match: love the way you lie

Group 1: love the way you lie

Group 2: the

Group 3: lie

正則表達式電路

jex.im可視化正則表達式:

https://img1.sycdn.imooc.com//65011b2c0001ec6609950210.jpg


查看完整回答
反對 回復 2023-09-13
?
九州編程

TA貢獻1785條經驗 獲得超4個贊

顯然,matcher.groupCount()在您的情況下返回 2,因此您只需構造兩個字符串的數組并將數字小于 2 的組復制到其中,即組 0(整個字符串)和組 1(“the”)。如果您matcher.groupCount()在整個代碼中添加 1,它將按預期工作。



查看完整回答
反對 回復 2023-09-13
  • 2 回答
  • 0 關注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號