qq_38198467 2018-12-03 04:36 采纳率: 0%
浏览 959

Java正则表达式无法匹配 ^匹配字符串开头 却冰崩匹配成功 为什么?

package regular_expression;

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

public class Test {
public static void main(String[] args) {

    Pattern pattern = Pattern.compile("^the");
    Matcher matcher1 = pattern.matcher("the is");
    Matcher matcher2 = pattern.matcher("there");
    boolean matches1 = matcher1.matches();
    boolean matches2 = matcher1.matches();
    System.out.println(matches1);
    System.out.println(matches2);
}

}
图片说明

  • 写回答

2条回答 默认 最新

  • 天涯云海 2018-12-03 19:20
    关注

    Pattern pattern = Pattern.compile("^the.+");
    Matcher matcher1 = pattern.matcher("the is");
    Matcher matcher2 = pattern.matcher("there");
    boolean matches1 = matcher1.matches();
    boolean matches2 = matcher1.matches();
    System.out.println(matches1);
    System.out.println(matches2);

    .+表示the后面可以是任意值

    评论
  • zhangpan_soft 2018-12-03 22:12
    关注

    你的正则说了一the开头,但是后面是设么规则没说,所以全部都是false,所以要修改正则为^the.*,这样可以匹配the,there,等等,也就是说以the开头,后面有0位或者多位

    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部