0

我有一个处理许多空格分隔字符串的操作,我正在为字符串匹配函数寻找一个正则表达式,如果第一个空格之前的前两个字符串以大写字母开头,它将触发传递,如果不是,则返回 false。

例子:

"AL_RIT_121 PA_YT_32 rit cell 22 pulse"

将作为前两个子字符串返回 true,并分别以大写字母AL_RIT_121PA_YT_32开头AP

"AL_RIT_252 pa_YT_21 mal cell reg 32 1 ri"

p在小写中返回 false。

4

5 回答 5

6
Pattern.compile("^\\p{Lu}\\S*\\s+\\p{Lu}")

将使用该.find()方法。没有理由matches在前缀测试中使用,但如果您有外部约束,只需执行

Pattern.compile("^\\p{Lu}\\S*\\s+\\p{Lu}.*", Pattern.DOTALL)

要打破这一点:

  1. ^匹配字符串的开头,
  2. \\p{Lu}匹配任何大写字母,
  3. \\S*匹配零个或多个非空格字符,包括_
  4. \\s+匹配一个或多个空格字符,并且
  5. 第二个\\p{Lu}匹配开始第二个单词的大写字母。

在第二个变体中,.*结合Pattern.DOTALL匹配输入的其余部分。

于 2012-11-19T17:10:35.947 回答
4

简单地string.matches("[A-Z]\\w+ [A-Z].*")

于 2012-11-19T17:11:06.453 回答
1

看一下这个:

    public static void main(String[] args) 
{
    String text = "AL_RIT_121 pA_YT_32 rit cell 22 pulse";

    boolean areFirstTwoWordsCapitalized = areFirstTwoWordsCapitalized(text);

    System.out.println("areFirstTwoWordsCapitalized = <" + areFirstTwoWordsCapitalized + ">");

}

private static boolean areFirstTwoWordsCapitalized(String text)
{
    boolean rslt = false;

    String[] words = text.split("\\s");

    int wordIndx = 0;

    boolean frstWordCap = false;
    boolean scndWordCap = false;

    for(String word : words)
    {
        wordIndx++;

        //System.out.println("word = <" + word + ">");

        Pattern ptrn = Pattern.compile("^[A-Z].+");

        Matcher mtchr = ptrn.matcher(word);

        while(mtchr.find())
        {
            String match = mtchr.group();

            //System.out.println("\tMatch = <" + match + ">");

            if(wordIndx == 1)
            {
                frstWordCap = true;
            }
            else if(wordIndx == 2)
            {
                scndWordCap = true;
            }
        }
    }

    rslt = frstWordCap && scndWordCap;

    return rslt;
}
于 2012-11-19T17:27:30.633 回答
1

You can use a specific regex if those two examples demonstrate your input format:

^(?:[A-Z]+_[A-Z]+_\d+\s*)+

Which means:

^           - Match the beginning of the string
(?:         - Start a non-capturing group (used to repeat the following)
    [A-Z]+  - Match one or more uppercase characters
    _       - Match an underscore
    [A-Z]+  - Match one or more uppercase characters
    _       - Match an underscore
    \d+     - Match one or more decimals (0-9)
    \s*     - Match zero or more space characters
)+          - Repeat the above group one or more times

You would use it in Java like this:

Pattern pattern = Pattern.compile("^(?:[A-Z]+_[A-Z]+_\\d+\\s*)+");
Matcher matcher = p.matcher( inputString);
if( matcher.matches()) {
    System.out.println( "Match found.");
}
于 2012-11-19T17:15:15.187 回答
1

尝试这个:

public class RegularExp 
{

    /**
     * @param args
     */
    public static void main(String[] args) {
        String regex = "[A-Z][^\\s.]*\\s[A-Z].*";
        String str = "APzsnnm lmn Dlld";
        System.out.println(str.matches(regex));

    }

}
于 2012-11-19T17:51:45.130 回答