gpt4 book ai didi

java - 拆分字符串,如果它有数字

转载 作者:行者123 更新时间:2023-11-29 06:32:46 25 4
gpt4 key购买 nike

大家好,好久没问了,

我有这个字符串,它由一个名字和一个数字组成例如。

String myString = "give11arrow123test2356read809cell1245cable1257give222..."

现在我要做的是在有数字的时候拆分它

我必须拆分它才能得到这样的结果

give11, arrow123, test2356, read809, cell1245, cable1257, give222, ....

我可以使用这段代码,但我找不到正确的正则表达式

String[] arrayString = myString.split("Regex")

感谢您的帮助。

最佳答案

您可以使用 lookarounds 的组合拆分字符串。

Lookarounds are zero-width assertions. They don't consume any characters on the string. The point of zero-width is the validation to see if a regex can or cannot be matched looking ahead or looking back from the current position, without adding them to the overall match.

String s = "give11arrow123test2356read809cell1245cable1257give222...";
String[] parts = s.split("(?<=\\d)(?=\\D)");
System.out.println(Arrays.toString(parts));

输出

[give11, arrow123, test2356, read809, cell1245, cable1257, give222, ...]

关于java - 拆分字符串,如果它有数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28734352/

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