以字母和数字拆分字符串
Split string in alphabet and number
任何人都可以帮助我一些正则表达式。
我想将以下字符串拆分为字母和数字。
例子
拆分后的字符串 ns01sp0001
应该是
ns01sp
和 0001
.
我尝试使用以下正则表达式。
String array[] = str.split("[^A-Z0-9]+|(?<=[A-Z])(?=[0-9])|(?<=[0-9])(?=[A-Z])");
大写为 return
[NS, 01, SP, 0001]
但小写是 return
[, 01, 0001] // not able to get alphabet.
有什么方法可以得到像
这样的输出吗
[NS01SP,0001] // if input = NS01SP0001
[ns01sp,0001] //if input = ns01sp0001.
您可以使用:
String tok[] = str.split("(?<=\D)(?=\d+\b)");
任何人都可以帮助我一些正则表达式。 我想将以下字符串拆分为字母和数字。
例子
拆分后的字符串 ns01sp0001
应该是
ns01sp
和 0001
.
我尝试使用以下正则表达式。
String array[] = str.split("[^A-Z0-9]+|(?<=[A-Z])(?=[0-9])|(?<=[0-9])(?=[A-Z])");
大写为 return
[NS, 01, SP, 0001]
但小写是 return
[, 01, 0001] // not able to get alphabet.
有什么方法可以得到像
这样的输出吗[NS01SP,0001] // if input = NS01SP0001
[ns01sp,0001] //if input = ns01sp0001.
您可以使用:
String tok[] = str.split("(?<=\D)(?=\d+\b)");