所以我有一个看起来像这样的字符串:
UPDATE 12.7543.81 ParmA="dk.asterix.org" [Denmark] (9.1) ParmB="de.asterix.org" [Germany] (1.0) Called=49xxxxxxx (GBH) Calling=45xxxxxxxx (LOA) Internal=0 State=2
UPDATE 12.7543.81 ParmB="de.asterix.org" [Germany] (1.0) ParmA="dk.asterix.org" [Denmark] (9.1) Called=49xxxxxxx (GBH) Calling=45xxxxxxxx (LOA) Internal=0 State=2
UPDATE 12.7543.81 Internal=0 State=2 ParmB="de.asterix.org" [Germany] (1.0) ParmA="dk.asterix.org" [Denmark] (9.1) Called=49xxxxxxx (GBH) Calling=45xxxxxxxx (LOA)
UPDATE 12.7543.81 Internal=0 State=2 ParmB="de.asterix.org" [Germany] (1.0) Calling=45xxxxxxxx (LOA) ParmA="dk.asterix.org" [Denmark] (9.1) Called=49xxxxxxx (GBH)
所有完全随机添加到列表中,但它们仍然遵循 1 个特定的块模式:
xxx = String
ddd = decimal
iii = integer
chunk 1: UPDATE
chunk 2: xxx.xxx.xxx
chunk x: ParmA="xxx" [xxx] (ddd)
chunk x: ParmB="xxx" [xxx] (ddd)
chunk x: Calling=xxx (xxx)
chunk x: Called=xxx (xxx)
chunk x: Internal=iii
chunk x: State=iii
我想将所有字符串数据提取到变量中,但是正则表达式不喜欢随机顺序,所以我正在考虑使用 split(""),并在每个 Word 中使用 cykle。
但是在我开始这样做之前,我想问一下是否有另一种方法来提取数据?
分离块的想法示例:
import java.util.ArrayList;
import java.util.List;
public class Test {
final static String[] lines = new String[]{
"UPDATE 12.7543.81 ParmA=\"dk.asterix.org\" [Denmark] (9.1) ParmB=\"de.asterix.org\" [Germany] (1.0) Called=49xxxxxxx (GBH) Calling=45xxxxxxxx (LOA) Internal=0 State=2",
"UPDATE 12.7543.81 ParmB=\"de.asterix.org\" [Germany] (1.0) ParmA=\"dk.asterix.org\" [Denmark] (9.1) Called=49xxxxxxx (GBH) Calling=45xxxxxxxx (LOA) Internal=0 State=2",
"UPDATE 12.7543.81 Internal=0 State=2 ParmB=\"de.asterix.org\" [Germany] (1.0) ParmA=\"dk.asterix.org\" [Denmark] (9.1) Called=49xxxxxxx (GBH) Calling=45xxxxxxxx (LOA)",
"UPDATE 12.7543.81 Internal=0 State=2 ParmB=\"de.asterix.org\" [Germany] (1.0) Calling=45xxxxxxxx (LOA) ParmA=\"dk.asterix.org\" [Denmark] (9.1) Called=49xxxxxxx (GBH)"
};
public static void main(String[] args){
for(String line : lines){
String[] parms = splitParm(line);
for(String parm : parms){
System.out.println(parm);
}
}
}
static public String[] splitParm(String text){
String[] textarr = text.split(" ");
List<String> parms = new ArrayList<>();
parms.add(textarr[0]); // UPDATE
parms.add(textarr[1]); // 12.7543.81
for(int i = 2;i<textarr.length;i++){
if(textarr[i].matches("^([A-Za-z]+)=([\\S ]+)$")){
parms.add(textarr[i]);
}
else{
parms.set(parms.size()-1, parms.get(parms.size()-1) + " "+textarr[i]);
}
}
return parms.toArray(new String[]{});
}
}