提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用正则表达式开始和结束资本
import java.util.regex.Matcher; import java.util.regex.Pattern;// from 时 代 J a v a - nowjava.com public class Main { public static void main(String[] args) { String inputStr = "this is a TEST tesT Test"; String[] stringArray = inputStr.split(" "); for (String s : stringArray) { String regex = "\\b[A-Z][a-zA-Z]*[A-Z]\\b"; // String regex = "([A-Z]\\w*[A-Z])"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(s); boolean found = matcher.find();