2012-12-17 7 views
-3

알겠습니다. 일주일 전에 게시 한 다른 질문에 대한 답변을 드리겠습니다. Unable to parse^character태그를 표시 할 수 없습니다.

내 현재 상황은 필요한 정규식을 구문 분석하고 후속 요소를 만들 수있게되었습니다. 각 요소 유형에 대한 태그를 만들었습니다. 1 | | 2 | 3^4 | B || 1 | 2 | 내 입력 문자열이 있습니다 예를 들어

^4^5

3 그래서이 문자열 내 태그

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 

입력은 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(); 
} } 
+2

질문 자체가 포함되어야합니다. 우리가 다른 질문을 읽을 것을 요구하지 마십시오. (이것은 포럼이 아니므로 실로 스레드가 아닙니다.) –

+0

은 코드를 추가했습니다 ... 나는 @jlordo가 아주 언젠가 이것에 대해 토론하고 있었기 때문에 또 다른 질문을 만들지를 물었습니다. – Sid

+0

별도의 질문을 만들고 그것에 링크하는 것이 좋습니다. * 우리 모두를 읽을 필요는 없습니다. –

답변

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); 
     } 
    } 
} 

하고 입력 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 
에 대한

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(); 
    } 
} 

출력 (

+0

@SiddharthSharma이 솔루션이 작동하지 않습니까? 동의하지 않는 이유는 무엇입니까? – jlordo

+0

@jlordo ... 예상대로 작동합니다. 조금 닦을 필요가 있습니다 ... 자고 있었으므로 받아 들일 수 없었습니다. – Sid

+0

입력을 한 줄 A | 1 | 2 | 3^4 | B || 1 | 2 | 3^4^5에서 여러 줄 입력으로 변환 중입니다. 여러 번의 시도 끝에 콘솔을 통해 수행 할 수 없다는 것을 깨달았습니다. 그래서 저는 입력 스타일을 파일 기반 입력으로 옮깁니다. 그것이 어떻게되는지 보자 :) – Sid

관련 문제