-3

好的,我正在关注我一周前发布的另一个问题。 无法解析 ^ 字符

我目前的情况是,现在我能够解析所需的正则表达式并创建后续元素。我为每种元素类型创建了标签。

例如,我的输入字符串是 A|1|2|3^4|B||1|2|3^4^5

所以我在这个字符串中的标签是 A 和 B

我的输出应该是(预期的)

A1      1 
A2      2
A3.1    3 
A3.2    4
B1      (BLANK) //not B
B2      1
B3      2
B4.1    3
B4.2    4
B4.3    5

B1 应该为空的原因是因为输入是 B||1|2。我目前的输出将是

element1    A
element2    1
element3.1  2
element3.2  3
element4    4
element5    B
element6     
element7.1  2
element7.2  3
element7.3  4
element7.4  5

基本上,我正在尝试构建一个 HL7 解析器。用准确的标签替换是为了避免混淆和保密。代码如下。

公共类解析器{

public static final String ELEMENT_DELIM_REGEX = "\\|";

public static void main(String[] args) {
    String input = "A|1|2|3^4|";
    String[] tokens = input.split(ELEMENT_DELIM_REGEX);
    Element[] elements = new Element[tokens.length];
    for (int i = 0; i < tokens.length; i++) {
        elements[i] = new Element(i + 1, tokens[i]);
    }
    for (Element element : elements) {
        System.out.println(element);
    }
}

}

Element.java

公共类元素{

public static final String SUB_ELEMENT_DELIM_REGEX = "\\^";

private int number;

private String[] content;

public Element(int number, String content) {
    this.number = number;
    this.content = content.split(SUB_ELEMENT_DELIM_REGEX);
}

@Override
public String toString() {
    if (content.length == 1) {
        return "Element " + number + "\t" + content[0];
    }
    StringBuilder str = new StringBuilder();
    for (int i = 0; i < content.length; i++) {
        str.append("Element " + number + "." + (i+1) + "\t" + content[i] + "\n");   
    }
    // Delete the last \n
    str.replace(str.length() - 1, str.length(), "");
    return str.toString();
} }
4

1 回答 1

1

可以通过对两个类进行一些小的更改来实现:(如果您不明白代码中发生了什么,请询问我,我只写了一些评论,但试图保持自我解释)

public class Parser {

    public static final String ELEMENT_DELIM_REGEX = "\\|";

    public static void main(String[] args) {
        String input = "A|1|2|3^4|B||1|2|3^4^5";
        String[] tokens = input.split(ELEMENT_DELIM_REGEX);
        List<Element> elements = new ArrayList<Element>();
        String currentTag = "";
        int elementCounter = 1;
        for (int i = 0; i < tokens.length; i++) {
            if (tokens[i].matches("\\p{Alpha}+")) {
                currentTag = tokens[i];
                elementCounter = 1;
                continue;
            }
            elements.add(new Element(elementCounter++, currentTag,  tokens[i]));
        }
        for (Element element : elements) {
            System.out.println(element);
        }
    }
}

public class Element {

    public static final String SUB_ELEMENT_DELIM_REGEX = "\\^";

    private int number;

    private String[] content;

    private String tag;

    public Element(int number, String tag, String content) {
        this.number = number;
        this.content = content.split(SUB_ELEMENT_DELIM_REGEX);
        this.tag = tag;
    }

    @Override
    public String toString() {
        if (content.length == 1) {
            return tag + "" + number + "\t" + content[0];
        }
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < content.length; i++) {
            str.append(tag + "" + number + "." + (i+1) + "\t" + content[i] + "\n");   
        }
        // Delete the last \n
        str.replace(str.length() - 1, str.length(), "");
        return str.toString();
    } 
}

输出(供您输入A|1|2|3^4|B||1|2|3^4^5

A1      1
A2      2
A3.1    3
A3.2    4
B1  
B2      1
B3      2
B4.1    3
B4.2    4
B4.3    5
于 2012-12-17T15:06:59.587 回答