Akashic Records

Reqular Expression in XML 본문

오래된글/Java

Reqular Expression in XML

Andrew's Akashic Records 2018. 4. 9. 13:07
728x90

regexml(Reqular expressions)은 Text에서 특정 Pattern(xml로 정의)에 Match되는 문자열을 축출하는 Java Library 입니다.

표준 Java API에서 정규표현식을 이용한 문자열 축출 방식 아래와 유사한 방식일 것입니다.

String url = "http://www.regexml.org:8080/sample/resource.html?param=1#anchor"; --> 축출 대상 Text

String regex = "^([A-Za-z]+):(?://)?([0-9.\-A-Za-z@]+)(?::(\d{1,5}))?(/[^\?#]+)?(?:\?([^#]*))?(?:#(.*))?$"; --> 축출할 문자열의 정규표현식

Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);

Mathcer matcher = pattern.matcher(url);

위의 예는 주어진 url에서 sheme, domain, port, resource, query string, anchor tag을 축출하는 암호문 같은 정규식입니다.

주석까지 포함한 정규식을 표현한다면 아래 같겠습니다.

^(#scheme (e.g., http\))([A-Za-z]+):(?://)?(#domain (e.g., www.regexml.org\))([0-9.\-A-Za-z@]+)(?::(#port number)(\d{1,5}))?

(#resource (e.g., /about/project/license.html\))(/[^\?#]+)?(#query string)(?:\?([^#]*))?(#URL anchor tag)(?:#(.*))?$

이것 또한 암호문 같습니다. regexml은 이런 암호문 같은 정규식을 쉽게 읽고, 만들수 있게 해주는 Opensource입니다.

또한, regexml은 정규식을 코드 외부에 xml로 정의하고, 하나의 xml에 여러 정규식을 정의 할수도 있습니다.

아래는 regexml 정규식 xml 입니다.(url_expression.xml)

<?xml version="1.0" encoding="UTF-8"?>

<regexml xmlns="http://schemas.regexml.org/expressions">

<expression id="url">

<start/>

<match equals="[A-Za-z]" max="*" capture="true"/> <!-- scheme (e.g., http) -->

<match equals=":"/>

<match equals="//" min="0"/> <!-- mailto: and news: URLs do not require forward slashes -->

<match equals="[0-9.\-A-Za-z@]" max="*" capture="true"/> <!-- domain (e.g., www.regexml.org) -->

<group min="0">

<match equals=":"/>

<match equals="\d" max="5" capture="true"/> <!-- port number -->

</group>

<group min="0" capture="true"> <!-- resource (e.g., /sample/resource) -->

<match equals="/"/>

<match except="[?#]" max="*"/>

</group>

<group min="0">

<match equals="?"/>

<match except="#" min="0" max="*" capture="true"/> <!-- query string -->

</group>

<group min="0">

<match equals="#"/>

<match equals="." min="0" max="*" capture="true"/> <!-- anchor tag -->

</group>

<end/>

</expression>

</regexml>


equals: 같은걸 찾아라.

except: 제외하라.

capture="true": 해당 문자열을 축출하라.

max="5": 같은게 최대 5개까지 존재 할수 있다.

min="0": 같은게 없을 수도 있다.

축출하는 Java Code는 "expressionFactory"만 제외하고 표준 API 사용과 동일합니다.

expressionFactory ef = new expressionFactory(new ClassPathResource("url_expression.xml"));

Pattern pattern = ef.getPattern("url"); //matches id attribute in <expression> element

Matcher matcher = pattern.matcher("http://www.regexml.org:8080/sample/resource.html?param=1#anchor");

if (matcher.find())

{

for (int i = 1; i <= matcher.groupCount(); i++)

{

System.out.println(matcher.group(i));

}

}


결과

http

www.regexml.org

8080

/sample/resource.html

param=1

anchor


정규표현식이라는게 처음 접할때는 굉장히 낮설지만 쫌 익숙해지면 불편함도 없지요.(정규표현식 그 출신 자체가 편리함을 지향하니깐요.)

regexml을 이렇게 정의하고 싶습니다.

"복잡한 정규 표현식을 실수 없이 빨리 만들 수 있는 유용한 툴. 단, 단순한 정규표현식을 사용한다거나, 정규표현식에 익숙한 개발자에게는 추천 하지 않는다."

728x90

'오래된글 > Java' 카테고리의 다른 글

sscache : SHOP.COM Cache System  (0) 2018.04.09
Sampling & Testing JUnit  (0) 2018.04.09
Powerful Fluent Interfaces is Now Easier than Ever: op4j 1.0 Released  (0) 2018.04.09
non-blocking 소켓  (0) 2018.04.09
Nail 이미지 생성 클래스  (0) 2018.04.09
Comments