gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 19:58:48 26 4
gpt4 key购买 nike

我有以下字符串“3/4Ton”。我想将其拆分为 -->

word[1] = 3/4 和 word[2] = 吨。

现在我的代码是这样的:-

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

它执行根据大写字母拆分字符串所需的任务,例如:-

字符串 = 机械输入

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

唯一的问题是它不保留数字或缩写或大写字母序列,这些字母不应该是单独的单词。谁能帮我解决我的正则表达式编码问题。

提前致谢...

最佳答案

实际上,您可以使用向前看和向后看仅在正则表达式中执行此操作(请参阅本页的特殊结构: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);
}

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

for(String word: myString.split(DIVIDER_PATTERN)){
System.out.println(word);
}

肖恩

关于java - 根据java中的模式拆分字符串 - 大写字母和数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2850487/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com