0

スペースで区切られた多くの文字列を処理する操作があります。最初のスペースの前の最初の2つの文字列が大文字で始まる場合はパスをトリガーし、そうでない場合はfalseを返す文字列一致関数の正規表現を探しています。

例:

"AL_RIT_121 PA_YT_32 rit cell 22 pulse"

最初の2つの部分文字列としてtrueを返し、それぞれ大文字とで始まりAL_RIT_121ますPA_YT_32AP

"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+1つ以上のスペース文字に一致し、
  5. 2番目\\p{Lu}は、2番目の単語で始まる大文字と一致します。

2番目のバリアントでは、.*と組み合わせてPattern.DOTALL残りの入力と一致します。

于 2012-11-19T17:10:35.947 に答える
4

単にstring.matches("[A-Z]\\w+ [A-Z].*")

于 2012-11-19T17:11:06.453 に答える
1

これらの2つの例が入力形式を示している場合は、特定の正規表現を使用できます。

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

つまり:

^           - 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

次のようにJavaで使用します。

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 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

これを試して:

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 に答える