正则表达式

ZHLEEBlander 2012-04-05 09:32:27
请问我想判断字符串以什么开头这样写对不对啊
String string = "theerwiiuo";
Pattern pattern = Pattern.compile("^the");
Matcher m = pattern.matcher(string);
System.out.print(m.matches());
...全文
108 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
因为你用的是 Matcher#matches 方法,这个方法要全部匹配正则表达式才会返回 true

你用 Matcher#lookingAt 方法就可以了
sdojqy1122 2012-04-05
  • 打赏
  • 举报
回复
你那句就匹配一个the,不是匹配以the开头的任意字符串。
fan_gangliang 2012-04-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

String string = "theerwiiuo";
Pattern pattern = Pattern.compile("^the.*");
Matcher m = pattern.matcher(string);
System.out.print(m.matches());
[/Quote]

正则表达式能解释下吗,之前的为何不对
ZHLEEBlander 2012-04-05
  • 打赏
  • 举报
回复
think very much
baby1986 2012-04-05
  • 打赏
  • 举报
回复
为什么不用String类的startsWith方法呢?
sdojqy1122 2012-04-05
  • 打赏
  • 举报
回复
String string = "theerwiiuo";
Pattern pattern = Pattern.compile("^the.*");
Matcher m = pattern.matcher(string);
System.out.print(m.matches());
sdojqy1122 2012-04-05
  • 打赏
  • 举报
回复
运行一下不就知道了?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧