简体   繁体   English

Java正则表达式可获取以大写字母开头并以特定单词结尾的单词

[英]Java Regular Expression to get word or words that start with capital letter and ends with a specific word

How do I write a regular expression that will match a word or a group of words that start with a capital letter and ends with a specific word. 我该如何写一个正则表达式,以匹配一个单词或一组以大写字母开头并以特定单词结尾的单词。

Examples: 例子:

string = {"the company is named Oracle Corporation", 
           "JP Morgan & Chase Corporation is under pressure"}

I need to get the following: "Oracle Corporation" and "JP Morgan & Chase Corporation" 我需要获得以下信息: "Oracle Corporation""JP Morgan & Chase Corporation"

How about 怎么样

'\s[A-Z].*Corporation\b'

\\s matches whitespace. \\s匹配空格。 [AZ] matches a capital letter. [AZ]匹配一个大写字母。 .* matches absolutely anything. .*绝对匹配任何东西。 Corporation matches "Corporation". Corporation匹配“公司”。 \\b matches the end of a word. \\b匹配单词的结尾。

See also: http://www.vogella.com/articles/JavaRegularExpressions/article.html 另请参阅: http : //www.vogella.com/articles/JavaRegularExpressions/article.html

THis might help you get started. 这可能会帮助您入门。 It is not a regex, but I think you'll have more flexibility with it. 它不是正则表达式,但我认为您会拥有更多的灵活性。

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());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM