java - split函数正则匹配,如何让前面不是数字的冒号作为分割符
巴扎黑
巴扎黑 2017-04-18 10:36:16
0
2
731
巴扎黑
巴扎黑

reply all(2)
Ty80

/:(?!d)/


ES6

> str="姓名:王尼玛 性别:男 播音时间:每天10:20PM";
> str.split(" ").forEach((s)=>console.log(s.split(/:(?!\d)/)))
[ '姓名', '王尼玛' ]
[ '性别', '男' ]
[ '播音时间', '每天10:20PM' ]
undefined
>

The regular expressions are almost the same, just use js to demonstrate~


java

import java.util.Arrays;
public class HelloWorld{
    public static void main(String[] args) {
        String[] sarr = "姓名:王尼玛 性别:男 播音时间:每天10:20PM".split(" ");
        String[][] Arr=new String[sarr.length][];
        for (int i=0;i<sarr.length;i++) {
            Arr[i]=sarr[i].split(":(?!\d)");
            sarr[i]=Arrays.toString(sarr[i].split(":(?!\d)"));
        }        
        System.out.println(Arrays.toString(sarr));
    }
}
//[[姓名, 王尼玛], [性别, 男], [播音时间, 每天10:20PM]]

Writing programs in Java is very cumbersome, like stacking up a bed and building a house~

It’s much easier to use python~

r'([^:]+):(?!d)([^ ]+)'

>>> import re
>>> s="姓 名:王尼玛 性别:男 播音时间:每天10:20PM";
>>> p=re.compile(r'([^:]+):(?!\d)([^ ]+)')
>>> p.findall(s)
[('姓 名', '王尼玛'), (' 性别', '男'), (' 播音时间', '每天10:20PM')]
刘奇
String str = "姓名:王尼玛 性别:男 播音时间:每天10:20PM";
List<String> list = new ArrayList<>();
for (String s : str.split(" ")) {
    int i = s.indexOf(":");
    list.add(s.substring(0, i));
    list.add(s.substring(i + 1));
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template