/:(?!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)); }
/:(?!d)/
ES6
The regular expressions are almost the same, just use js to demonstrate~
java
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)([^ ]+)'