将字符串解析为多个变量

时间:2013-08-08 12:15:32

标签: java regex split

所以我有一个字符串,看起来像这样:

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(“”)和cykle通过每个Word。

但是在我开始这样做之前,我可以问一下,是否有另一种提取数据的方式?

分离块的想法示例:

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[]{});
    }
}

4 个答案:

答案 0 :(得分:1)

一个想法是为每个块使用单独的正则表达式 - 然后使用所有这些表达式处理每一行。

答案 1 :(得分:1)

以下代码应该有效:

String s = "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";
Pattern p = Pattern.compile("([^\\s]+)\\s+([^\\s]+)\\s+");
Pattern p1 = Pattern.compile("([^\\s]+)\\s*=\\s*\"?([^\\s\"]+)\"?");
Matcher m = p.matcher(s);
if (m.find() && m.groupCount() == 2) {
    System.out.printf("%s%n%s%n", m.group(1), m.group(2) );
    Matcher m1 = p1.matcher(s.substring(m.end(2)));
    while (m1.find()) {
        System.out.printf("\t%s=%s%n", m1.group(1), m1.group(2) );
    }
}

<强>输出:

UPDATE
12.7543.81 
    ParmA=dk.asterix.org
    ParmB=de.asterix.org
    Called=49xxxxxxx
    Calling=45xxxxxxxx
    Internal=0
    State=2

答案 2 :(得分:0)

不要使用正则表达式。只需通过拆分和循环,您就可以获得更易维护的代码。

答案 3 :(得分:0)

CommandUpdate.java

 import java.util.ArrayList;
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    public class CommandUpdate {
        private List<ParamSingle> parms;

        public CommandUpdate(String commandstring) {
            parms = generateParms(splitParm(commandstring));
        }

        public ParamSingle[] getParms(){
            return parms.toArray(new ParamSingle[]{});
        }

        // ****************************************************************//
        // Class Tools
        // ****************************************************************//
        private 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[] {});
        }

        private List<ParamSingle> generateParms(String[] parmstrings) {
            List<ParamSingle> parms = new ArrayList<>();

            Pattern pv1 = Pattern.compile("^([A-Za-z]+)=([0-9]+)$");
            Pattern pv2 = Pattern.compile("^([\\S]+)=([\\S]+) \\(([\\S]+)\\)$");
            Pattern pv3 = Pattern.compile("^([A-Za-z]+)=\"([\\S ]+)\" \\[([\\S ]+)\\] \\(([0-9\\.]+)\\)");
            Matcher match;

            for (String parmstring : parmstrings) {
                if ((match = pv1.matcher(parmstring)).find()) {
                    parms.add(new Param1Value<Integer>(match.group(1), Integer.parseInt(match.group(2))));
                } else if ((match = pv2.matcher(parmstring)).find()) {
                    parms.add(new Param2Value<String,String>(match.group(1), match.group(2),match.group(3)));
                } else if ((match = pv3.matcher(parmstring)).find()) {
                    parms.add(new Param3Value<String,String,Float>(match.group(1), match.group(2),match.group(3),Float.parseFloat(match.group(4))));
                }
            }

            return parms;
        }

        // ****************************************************************//
        // Structure Classes
        // ****************************************************************//

        public class Param3Value<T, T2, T3> extends Param2Value<T, T2> {
            private T3 value3;

            public Param3Value(String name, T value, T2 value2, T3 value3) {
                super(name, value, value2);
                setValue3(value3);
            }

            public T3 getValue3() {
                return this.value3;
            }

            public void setValue3(T3 value3) {
                this.value3 = value3;
            }
        }

        public class Param2Value<T, T2> extends Param1Value<T> {
            private T2 value2;

            public Param2Value(String name, T value, T2 value2) {
                super(name, value);
                setValue2(value2);
            }

            public T2 getValue2() {
                return this.value2;
            }

            public void setValue2(T2 value2) {
                this.value2 = value2;
            }
        }

        public class Param1Value<T> extends ParamSingle {
            private T value;

            public Param1Value(String name, T value) {
                super(name);
                setValue(value);
            }

            public T getValue() {
                return this.value;
            }

            public void setValue(T value) {
                this.value = value;
            }
        }

        public class ParamSingle {

            private String name;

            public ParamSingle(String name) {
                setName(name);
            }

            public String getName() {
                return this.name;
            }

            public void setName(String name) {
                this.name = name;
            }
        }
    }

Test.java

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) {

        CommandUpdate[] cus = new CommandUpdate[] {
                new CommandUpdate(lines[0]), new CommandUpdate(lines[1]),
                new CommandUpdate(lines[2]), new CommandUpdate(lines[3]) };

        for (CommandUpdate cu : cus) {
            for (CommandUpdate.ParamSingle ps : cu.getParms()) {
                if (ps instanceof CommandUpdate.Param3Value) {
                    CommandUpdate.Param3Value d = (CommandUpdate.Param3Value) ps;
                    System.out.println("Data: " + d.getName() + " Value:"
                            + d.getValue() + " Value2:" + d.getValue2()
                            + " Value3:" + d.getValue3());
                } else if (ps instanceof CommandUpdate.Param2Value) {
                    CommandUpdate.Param2Value d = (CommandUpdate.Param2Value) ps;
                    System.out.println("Data: " + d.getName() + " Value:"
                            + d.getValue() + " Value2:" + d.getValue2());
                } else if (ps instanceof CommandUpdate.Param1Value) {
                    CommandUpdate.Param1Value d = (CommandUpdate.Param1Value) ps;
                    System.out.println("Data: " + d.getName() + " Value:"
                            + d.getValue());
                }
            }
        }
    }
}