用字母和数字拆分字符串 [英] Split string in alphabet and number
本文介绍了用字母和数字拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有人能够帮助我使用一些正则表达式。
我想将以下字符串拆分为字母和数字。
Would anyone be able to assist me with some regex. I want to split the following string into alphabet and number.
示例
字符串 ns01sp0001
拆分后,它应该是
ns01sp
和 0001
。
我尝试使用以下正则表达式。
I tried with below regex.
String array[] = str.split("[^A-Z0-9]+|(?<=[A-Z])(?=[0-9])|(?<=[0-9])(?=[A-Z])");
对于大写的回报
[NS, 01, SP, 0001]
但是小写它的回报
[, 01, 0001] // not able to get alphabet.
有没有办法获得输出
[NS01SP,0001] // if input = NS01SP0001
[ns01sp,0001] //if input = ns01sp0001.
推荐答案
您可以使用:
String tok[] = str.split("(?<=\\D)(?=\\d+\\b)");
这篇关于用字母和数字拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文