根据java中的模式拆分字符串 - 大写字母和数字

leb*_*lev 5 java regex string

我有以下字符串"3/4Ton".我想将它拆分为 - >

word [1] = 3/4,word [2] = Ton.

现在我的代码看起来像这样: -

Pattern p = Pattern.compile("[A-Z]{1}[a-z]+");
Matcher m = p.matcher(line);
while(m.find()){
    System.out.println("The word --> "+m.group());
    }
Run Code Online (Sandbox Code Playgroud)

它执行基于大写字母分割字符串所需的任务,如: -

String = MachineryInput

字[1] =机械,字[2] =输入

唯一的问题是它不保留,数字或缩写或大写字母序列不是单独的单词.有人可以用我的正则表达式编码问题帮助我.

提前致谢...

Sea*_*oyd 5

实际上,您可以使用前瞻和后瞻单独在正则表达式中执行此操作(请参阅本页上的特殊构造:http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html

/**
 * We'll use this pattern as divider to split the string into an array.
 * Usage: myString.split(DIVIDER_PATTERN);
 */
private static final String DIVIDER_PATTERN =

        "(?<=[^\\p{Lu}])(?=\\p{Lu})"
                // either there is anything that is not an uppercase character
                // followed by an uppercase character

                + "|(?<=[\\p{Ll}])(?=\\d)"
        // or there is a lowercase character followed by a digit

        ;

@Test
public void testStringSplitting() {
    assertEquals(2, "3/4Word".split(DIVIDER_PATTERN).length);
    assertEquals(7, "ManyManyWordsInThisBigThing".split(DIVIDER_PATTERN).length);
    assertEquals(7, "This123/4Mixed567ThingIsDifficult"
                        .split(DIVIDER_PATTERN).length);
}
Run Code Online (Sandbox Code Playgroud)

所以你可以做的是这样的:

for(String word: myString.split(DIVIDER_PATTERN)){
    System.out.println(word);
}
Run Code Online (Sandbox Code Playgroud)

肖恩