或数字和字母之间

或数字和字母之间

本文介绍了如何在字母和数字之间(或数字和字母之间)拆分字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出一种在java中拆分字符串的方法,该方法遵循如下模式:

I'm trying to work out a way of splitting up a string in java that follows a pattern like so:

String a = "123abc345def";

此结果应如下:

x[0] = "123";
x[1] = "abc";
x[2] = "345";
x[3] = "def";

然而,我完全不知道如何实现这一目标。请有人帮帮我吗?我尝试在网上搜索类似的问题,但在搜索中很难正确地说出来。

However I'm completely stumped as to how I can achieve this. Please can someone help me out? I have tried searching online for a similar problem, however it's very difficult to phrase it correctly in a search.

请注意:字母&数字可能会有所不同(例如,可能有一个像'1234a5bcdef'这样的字符串)

Please note: The number of letters & numbers may vary (e.g. There could be a string like so '1234a5bcdef')

推荐答案

您可以尝试拆分(?< = \D)(?= \d)|(?< = \d)(?= \ D),如:

str.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");

它匹配数字和非数字之间的位置(按任意顺序)。

It matches positions between a number and not-a-number (in any order).

这篇关于如何在字母和数字之间(或数字和字母之间)拆分字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 01:03