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

Split String if it has number

大家好,我已经有一段时间没问另一个问题了,

我有这个由名称和数字组成的字符串 例如

String myString = "give11arrow123test2356read809cell1245cable1257give222..."

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

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

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

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

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

感谢您的帮助。

使用这个正则表达式进行拆分

String regex = "(?<=\d)(?=\D)";

您可以使用 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 中使用正则表达式,但此表达式符合您在 www.rubular.com

中的需要
([A-Za-z]+[0-9]+)