How can I split a string A000101
as A000 and 101
, 000101 as 000 and 101
using the same regular expression.
我如何使用相同的正则表达式将字符串A000101作为A000和101、000101和101分开。
I tried with something like this "A000101".split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)&(?!=0)")
but output is A and 000101
我试着用这样.split(“A000101(? < = \ \ D)(? = \ \ D)|(? < = \ \ D)(? = \ \ D)&(? ! = 0)”),但输出是一个和000101个
edit :
编辑:
Can I get A0000
and 0
from A00000
using the same logic ?
我能用同样的逻辑从A00000中得到A0000和0吗?
2 个解决方案
#1
2
It seems easier to me to use a Matcher
instead of split
:
对我来说,使用一个Matcher而不是split:
String str = "A000101";
Pattern p = Pattern.compile("([^1-9]*)([1-9]\\d*)");
Matcher m = p.matcher(str);
if (m.matches()) {
String prec = m.group(1);
String post = m.group(2);
}
#2
1
You may try this pattern as well:
你也可以试试这个模式:
([^0]*0*)(\d+)
Each group should give you one part of the string that you want in split.
每个组应该给你一个你想要分割的字符串的一部分。
Demo
#1
2
It seems easier to me to use a Matcher
instead of split
:
对我来说,使用一个Matcher而不是split:
String str = "A000101";
Pattern p = Pattern.compile("([^1-9]*)([1-9]\\d*)");
Matcher m = p.matcher(str);
if (m.matches()) {
String prec = m.group(1);
String post = m.group(2);
}
#2
1
You may try this pattern as well:
你也可以试试这个模式:
([^0]*0*)(\d+)
Each group should give you one part of the string that you want in split.
每个组应该给你一个你想要分割的字符串的一部分。