java中判断一个单词是否以a开头_查找Java中所有以'a'开头的单词

通过使用Java中的正则表达式,可以在字符串中找到所有以a开头的单词。正则表达式是可以使用特定模式语法匹配其他字符串的字符序列。在具有许多类的java.util.regex包中提供了正则表达式,但最重要的是Pattern类和Matcher类。

给出一个使用正则表达式查找所有以“ a”开头的单词的程序,如下所示:

示例import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class Demo {

public static void main(String args[]) throws Exception {

String str = "This is an apple";

String regex = "\\ba\\w*\\b";

Pattern p = Pattern.compile(regex);

Matcher m = p.matcher(str);

String word = null;

System.out.println("The input string is: " + str);

System.out.println("The Regex is: " + regex + "\r\n");

System.out.println("以上述字符串中的a开头的单词是:");

while (m.find()) {

word = m.group();

System.out.println(word);

}

if (word == null) {

System.out.println("There are no words that start with a");

}

}

}

输出结果The input string is: This is an apple

The Regex is: \ba\w*\b

以上述字符串中的a开头的单词是:

an

apple

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值