Java正则表达式,用于获取以大写字母开头并以特定单词结尾的一个或多个单词
如何编写一个正则表达式来匹配以大写字母开头并以特定单词结尾的一个单词或一组单词 示例:Java正则表达式,用于获取以大写字母开头并以特定单词结尾的一个或多个单词,java,regex,Java,Regex,如何编写一个正则表达式来匹配以大写字母开头并以特定单词结尾的一个单词或一组单词 示例: string = {"the company is named Oracle Corporation", "JP Morgan & Chase Corporation is under pressure"} 我需要得到以下信息:甲骨文公司和摩根大通公司 '\s[A-Z].*Corporation\b' \s匹配空格。[A-Z]匹配大写字母..*绝对匹配任何东西。公司匹配公司
string = {"the company is named Oracle Corporation",
"JP Morgan & Chase Corporation is under pressure"}
我需要得到以下信息:甲骨文公司和摩根大通公司
'\s[A-Z].*Corporation\b'
\s匹配空格。[A-Z]匹配大写字母..*绝对匹配任何东西。公司匹配公司\b匹配一个单词的结尾
另请参见:这可能有助于您开始学习。它不是正则表达式,但我认为你会有更多的灵活性
public class Test {
public static void main(String[] args) {
String test = "the company is named Oracle Corporation, and JP Morgan & Chase Corporation is under pressure";
String[] split = test.split("\\s");
StringBuilder sb = new StringBuilder();
for (String s : split) {
if (s.substring(0, 1).matches("[A-Z&]")) {
sb.append(s).append(" ");
}
}
System.out.println(sb.toString());
}
}
我当前的正则表达式是\\b?:\\p{Lu}\\p{L}*\\W+{0,2}?Corporation\\b像您这样的人对名称小写的公司不感兴趣。