在Java中使用正则表达式划分数字列表
我有一个由数字列表组成的字符串,如图所示:在Java中使用正则表达式划分数字列表,java,regex,Java,Regex,我有一个由数字列表组成的字符串,如图所示: 000000900100103010330200000005677890212126000020 使用单个正则表达式,我希望得到许多将原始字符串划分为不同长度的子字符串的结果。 我是说 00000009 001 001 03 01 033 02 00000005677890212126 00002 0 因此,我需要有这些不同的“组”(我希望它是正确使用的词) 00000009 001 001 03 01 033 02 0000000 567789
000000900100103010330200000005677890212126000020
使用单个正则表达式,我希望得到许多将原始字符串划分为不同长度的子字符串的结果。
我是说
00000009 001 001 03 01 033 02 00000005677890212126 00002 0
因此,我需要有这些不同的“组”(我希望它是正确使用的词)
- 00000009
- 001
- 001
- 03
- 01
- 033
- 02
- 0000000 5677890212126
- 00002
- 0
[0-9]{8}[0-9]{3}[0-9]{3}[0-9]{2}...
但是它当然不起作用您需要使用
模式
,如果找到了它,请使用Matcher.groupCount
和Matcher.group(inti)
静态最终模式p=Pattern.compile(
"([0-9]{8})"
+"([0-9]{3})"
+"([0-9]{3})"
+"([0-9]{2})"
+"([0-9]{2})"
+"([0-9]{3})"
+"([0-9]{2})"
+"([0-9]{20})"
+"([0-9]{5})"
+"([0-9]{1})");
私有无效测试(字符串[]args){
//注意:我在开始时又添加了一个0。
匹配器m=p.Matcher(“0000000 9001001030103302000005677890212126000020”);
if(m.find()){
对于(int i=1;i“(\\d{+i+”})”)
.collect(collector.joining());
您需要使用模式,如果找到该模式,请使用Matcher.groupCount
和Matcher.group(inti)
静态最终模式p=Pattern.compile(
"([0-9]{8})"
+"([0-9]{3})"
+"([0-9]{3})"
+"([0-9]{2})"
+"([0-9]{2})"
+"([0-9]{3})"
+"([0-9]{2})"
+"([0-9]{20})"
+"([0-9]{5})"
+"([0-9]{1})");
私有无效测试(字符串[]args){
//注意:我在开始时又添加了一个0。
匹配器m=p.Matcher(“0000000 9001001030103302000005677890212126000020”);
if(m.find()){
对于(int i=1;i“(\\d{+i+”})”)
.collect(collector.joining());
我喜欢上面的答案,这里有一种没有regex的替代方法,使用良好的旧for循环:
public static List<String> splitString(String inputString, int... lengths) {
List<String> substrings = new ArrayList<String>();
int start = 0;
int end = 0;
for(int length : lengths) {
start = end;
end = start + length;
String substring = inputString.substring(start, end);
substrings.add(substring);
}
return substrings;
}
private void test(String[] args) {
String s = "0000000900100103010330200000005677890212126000020";
List<String> list = splitString(s,8,3,3,2,2,3,2,20,5,1);
}
公共静态列表拆分字符串(字符串inputString,int…length){
List substring=new ArrayList();
int start=0;
int end=0;
用于(整数长度:长度){
开始=结束;
结束=开始+长度;
String substring=inputString.substring(开始、结束);
子字符串。添加(子字符串);
}
返回子串;
}
私有无效测试(字符串[]args){
字符串s=“0000000 9001001030103302000005677890212126000020”;
列表=拆分字符串(s,8,3,3,2,2,3,2,20,5,1);
}
我喜欢上面的答案,这里有一种没有regex的替代方法,使用良好的旧for循环:
public static List<String> splitString(String inputString, int... lengths) {
List<String> substrings = new ArrayList<String>();
int start = 0;
int end = 0;
for(int length : lengths) {
start = end;
end = start + length;
String substring = inputString.substring(start, end);
substrings.add(substring);
}
return substrings;
}
private void test(String[] args) {
String s = "0000000900100103010330200000005677890212126000020";
List<String> list = splitString(s,8,3,3,2,2,3,2,20,5,1);
}
公共静态列表拆分字符串(字符串inputString,int…length){
List substring=new ArrayList();
int start=0;
int end=0;
用于(整数长度:长度){
开始=结束;
结束=开始+长度;
String substring=inputString.substring(开始、结束);
子字符串。添加(子字符串);
}
返回子串;
}
私有无效测试(字符串[]args){
字符串s=“0000000 9001001030103302000005677890212126000020”;
列表=拆分字符串(s,8,3,3,2,2,3,2,20,5,1);
}
好吧,您实际上并没有在所示的正则表达式中进行分组,试试([0-9]{8})([0-9]{3})([0-9]{3})([0-9]{2})…
。但是,为什么您一开始就想使用正则表达式呢?难道“0000000 5677890212126”不包含两个匹配项(“0000000 567789”和“0212126”)吗?听起来好像您想要类似的东西(0+\d*?)(?=0 |$)
?如果每个元素的长度是固定的,最好使用字符串#substring(),而不是正则表达式。好吧,您实际上并不是在显示的正则表达式中分组,请尝试([0-9]{8})([0-9]{3})([0-9]{3})([0-9]{2})
。但是,为什么您一开始就想使用正则表达式呢?000000218902126不是吗包含两个匹配项(“0000000 567789”和“0212126”)?对我来说,听起来好像您想要的是符合(0+\d*?)(?=0 |$)
?如果每个元素的长度是固定的,最好使用String#substring(),而不是使用regex.UpVote添加零并使用java 8方法构造regex.UpVote添加零并使用java 8方法构造regex。
public static List<String> splitString(String inputString, int... lengths) {
List<String> substrings = new ArrayList<String>();
int start = 0;
int end = 0;
for(int length : lengths) {
start = end;
end = start + length;
String substring = inputString.substring(start, end);
substrings.add(substring);
}
return substrings;
}
private void test(String[] args) {
String s = "0000000900100103010330200000005677890212126000020";
List<String> list = splitString(s,8,3,3,2,2,3,2,20,5,1);
}