블로그 이미지
올해목표 // 10월 어학연수 떠나자~ 자수씨

카테고리

전체글 (1457)
Brand New! (28)
주절주절 (213)
MOT (11)
해외쇼핑 (49)
쇼핑노트 (150)
취미생활 (94)
iPhone (4)
Eclipse (121)
Google (83)
Spring (31)
JAVA (176)
JavaScript (59)
WEB (49)
Database (20)
OS (26)
Tools (8)
Tips (26)
IT정보 (1)
Book (21)
Programming (37)
외부행사 (43)
주변인들 (17)
여행노트 (60)
학교생활 (30)
회사생활 (52)
사회생활 (5)
외국어공부 (12)
잡동사니 (30)
Total
Today
Yesterday
 
04-20 08:40
 

달력

« » 2024.4
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
 

최근에 올라온 글

최근에 달린 댓글


역시 정규식이 짱!!!





Posted by 자수씨
, |

단순하게 HTML의 공백을   로 변환하는 로직은 아래와 같습니다.


하지만 이대로 사용한다면 문제가 있습니다. 바로 태그 내에 속성을 사용하면서 사용한 공백들도   로 바뀌어 버리게 됩니다.

[변환 전]
<div style="width=100%; height=100%;">태그  확인</div>

[변환 후]
<div&nbsp;style="width=100%;&nbsp;height=100%;">태그&nbsp;&nbsp;확인</div>



정규식을 사용해서 쉽게 변환하려 하였으나... 제가 아는 지식 내에서는 방법이 보이지 않아... 원초적인 방법을 사용하였습니다.


[변환 전]
<div style="width=100%; height=100%;">태그  확인</div>

[변환 후]
<div style="width=100%; height=100%;">태그&nbsp;&nbsp;확인</div>


흠... 이런 방법밖에 없을까요...

Posted by 자수씨
, |

















아래와 같은 코드가 있습니다.


이 코드를 작성한 작자는 datas 가 8 개의 요소를 가지고 있는 배열이기를 원합니다.



오묘해... 오묘해...

Posted by 자수씨
, |

현재 개발하고 있는 시스템에서 첨부파일을 다운로드할 경우 공백이 '+' 로 바뀌어서 저장이 됩니다.

예를들어 "불친절한 자수씨의 블로그.pdf" 라는 파일이라면 "불친절한+자수씨의+블로그.pdf" 라는 파일로 저장이 됩니다.

원인은 한글 파일명을 지원하기 위해 URLEncoder 를 사용하는데, 이것이 공백을 '+' 로 만드는 범인이였습니다.

기존 방식은 아래와 같았을 것 입니다.

개선 방법은 URLEncoding 후 '+' 로 변경된 공백을 '%20' 으로 다시 변경합니다.


'+' 는 정규식의 수량을 나타내는 것이기 때문에 역슬래쉬가 앞에 붙어야 합니다.

원래 있던 '+' 가 걱정되시는 분들은 걱정할 필요가 없습니다.
URLEncoder.encode() 메소드에 의해 '%2B' 로 변환이 됩니다.

어떻게 생각해보면 쉬운 것이였는데 신경을 못 쓰다보니 불편함을 안고 살았던 것 같습니다.

Posted by 자수씨
, |

HTML 의 style 속성을 가지고 이리저리 만져볼 일이 생겨서 StyleSheet 라는 클래스를 만들어 보았습니다.


정규식을 이용하여 style 속성을 분리하여 Map 에 담아서 슥삭슥삭 합니다.


[테스트 코드]

[출력 결과]
style-name: 바탕글; margin-left: 0.0pt; margin-right: 0.0pt; margin-top: 0.0pt; margin-bottom: 0.0pt; text-align: justify; text-indent: 0.0pt; line-height: 160%; font-size: 10.0pt; font-family: 굴림; letter-spacing: -1.5pt; font-weight: normal; font-style: normal; color: #000000; 
 


쓰면서 슬슬 고쳐나가야 할 듯 합니다.

Posted by 자수씨
, |

아래와 같은 문서가 있습니다.
P.HStyle0, LI.HStyle0, DIV.HStyle0
 {style-name:"바탕글"; margin-left:0.0pt; margin-right:0.0pt; margin-top:0.0pt; margin-bottom:0.0pt; text-align:justify; text-indent:0.0pt; line-height:160%; font-size:10.0pt; font-family:바탕; letter-spacing:0.0pt; font-weight:"normal"; font-style:"normal"; color:#000000;}
P.HStyle1, LI.HStyle1, DIV.HStyle1
 {style-name:"본문"; margin-left:15.0pt; margin-right:0.0pt; margin-top:0.0pt; margin-bottom:0.0pt; text-align:justify; text-indent:0.0pt; line-height:160%; font-size:10.0pt; font-family:바탕; letter-spacing:0.0pt; font-weight:"normal"; font-style:"normal"; color:#000000;}
P.HStyle2, LI.HStyle2, DIV.HStyle2
 {style-name:"개요 1"; margin-left:10.0pt; margin-right:0.0pt; margin-top:0.0pt; margin-bottom:0.0pt; text-align:justify; text-indent:0.0pt; line-height:160%; font-size:10.0pt; font-family:바탕; letter-spacing:0.0pt; font-weight:"normal"; font-style:"normal"; color:#000000;}
P.HStyle3, LI.HStyle3, DIV.HStyle3
 {style-name:"개요 2"; margin-left:20.0pt; margin-right:0.0pt; margin-top:0.0pt; margin-bottom:0.0pt; text-align:justify; text-indent:0.0pt; line-height:160%; font-size:10.0pt; font-family:바탕; letter-spacing:0.0pt; font-weight:"normal"; font-style:"normal"; color:#000000;}
P.HStyle4, LI.HStyle4, DIV.HStyle4
 {style-name:"개요 3"; margin-left:30.0pt; margin-right:0.0pt; margin-top:0.0pt; margin-bottom:0.0pt; text-align:justify; text-indent:0.0pt; line-height:160%; font-size:10.0pt; font-family:바탕; letter-spacing:0.0pt; font-weight:"normal"; font-style:"normal"; color:#000000;}
P.HStyle5, LI.HStyle5, DIV.HStyle5
 {style-name:"개요 4"; margin-left:40.0pt; margin-right:0.0pt; margin-top:0.0pt; margin-bottom:0.0pt; text-align:justify; text-indent:0.0pt; line-height:160%; font-size:10.0pt; font-family:바탕; letter-spacing:0.0pt; font-weight:"normal"; font-style:"normal"; color:#000000;}
P.HStyle6, LI.HStyle6, DIV.HStyle6
 {style-name:"개요 5"; margin-left:50.0pt; margin-right:0.0pt; margin-top:0.0pt; margin-bottom:0.0pt; text-align:justify; text-indent:0.0pt; line-height:160%; font-size:10.0pt; font-family:바탕; letter-spacing:0.0pt; font-weight:"normal"; font-style:"normal"; color:#000000;}
P.HStyle7, LI.HStyle7, DIV.HStyle7
 {style-name:"개요 6"; margin-left:60.0pt; margin-right:0.0pt; margin-top:0.0pt; margin-bottom:0.0pt; text-align:justify; text-indent:0.0pt; line-height:160%; font-size:10.0pt; font-family:바탕; letter-spacing:0.0pt; font-weight:"normal"; font-style:"normal"; color:#000000;}
P.HStyle8, LI.HStyle8, DIV.HStyle8
 {style-name:"개요 7"; margin-left:70.0pt; margin-right:0.0pt; margin-top:0.0pt; margin-bottom:0.0pt; text-align:justify; text-indent:0.0pt; line-height:160%; font-size:10.0pt; font-family:바탕; letter-spacing:0.0pt; font-weight:"normal"; font-style:"normal"; color:#000000;}
P.HStyle9, LI.HStyle9, DIV.HStyle9
 {style-name:"쪽 번호"; margin-left:0.0pt; margin-right:0.0pt; margin-top:0.0pt; margin-bottom:0.0pt; text-align:justify; text-indent:0.0pt; line-height:160%; font-size:10.0pt; font-family:굴림; letter-spacing:0.0pt; font-weight:"normal"; font-style:"normal"; color:#000000;}
P.HStyle10, LI.HStyle10, DIV.HStyle10
 {style-name:"머리말"; margin-left:0.0pt; margin-right:0.0pt; margin-top:0.0pt; margin-bottom:0.0pt; text-align:justify; text-indent:0.0pt; line-height:150%; font-size:9.0pt; font-family:굴림; letter-spacing:0.0pt; font-weight:"normal"; font-style:"normal"; color:#000000;}
P.HStyle11, LI.HStyle11, DIV.HStyle11
 {style-name:"각주"; margin-left:13.1pt; margin-right:0.0pt; margin-top:0.0pt; margin-bottom:0.0pt; text-align:justify; text-indent:-13.1pt; line-height:130%; font-size:9.0pt; font-family:바탕; letter-spacing:0.5pt; font-weight:"normal"; font-style:"normal"; color:#000000;}
P.HStyle12, LI.HStyle12, DIV.HStyle12
 {style-name:"미주"; margin-left:13.1pt; margin-right:0.0pt; margin-top:0.0pt; margin-bottom:0.0pt; text-align:justify; text-indent:-13.1pt; line-height:130%; font-size:9.0pt; font-family:바탕; letter-spacing:0.5pt; font-weight:"normal"; font-style:"normal"; color:#000000;}
P.HStyle13, LI.HStyle13, DIV.HStyle13
 {style-name:"메모"; margin-left:0.0pt; margin-right:0.0pt; margin-top:0.0pt; margin-bottom:0.0pt; text-align:justify; text-indent:0.0pt; line-height:160%; font-size:9.0pt; font-family:굴림; letter-spacing:0.5pt; font-weight:"normal"; font-style:"normal"; color:#000000;}

여기에서 P 스타일의 이름과 속성들을 개별적으로 뽑으려면 어떻게 할까요?

P.HStyle0, LI.HStyle0, DIV.HStyle0
 {style-name:"바탕글"; margin-left:0.0pt; margin-right:0.0pt; margin-top:0.0pt; margin-bottom:0.0pt; text-align:justify; text-indent:0.0pt; line-height:160%; font-size:10.0pt; font-family:바탕; letter-spacing:0.0pt; font-weight:"normal"; font-style:"normal"; color:#000000;}

예를 들면, 위의 내용으로 아래의 데이터를 뽑아내는 것 입니다.

{ styleName: "HStyle0", style: "style-name:\"바탕글\"; margin-left:0.0pt; margin-right:0.0pt; margin-top:0.0pt; margin-bottom:0.0pt; text-align:justify; text-indent:0.0pt; line-height:160%; font-size:10.0pt; font-family:바탕; letter-spacing:0.0pt; font-weight:"normal"; font-style:\"normal\"; color:#000000;" }




정규식만 잘 이용하면 CSS 파서 만드는 것도 어렵지 않을 것 같네요~
그 잘 이용하는게 어려울 뿐이죠...



Posted by 자수씨
, |

원본           : (2010. 03. 20.)
추출 데이터 : 2010-03-20

어떤 방법으로 해당 데이터를 추출할 수 있을까요?





Posted by 자수씨
, |

HTML 을 XML 파서로 파싱해서 객체화 시킬 일이 있어 여러 가지 실험해 보다가 쓸만한 팁을 정리합니다.

HTML 의 경우는 XML 과는 다르게 태그를 닫지 않거나 attribute 를 큰 따옴표로 감싸지 않아도 브라우저에서 확인이 가능합니다. 이렇기 때문에 HTML 을 XML 파서로 파싱하려면 큰 따옴표로 묶이지 않은 부분을 변경해 주어야 합니다.
(실제로 작업을 해보지는 않았으나 태그를 닫지 않는 것을 수정하려면 문서 전체를 문자열 단위로 파싱을 해야 가능할 것 같습니다.)

[TYPE_A]
<P class=PStyle0>
...
</P>

[TYPE_B]
<P class="PStyle0">
...
</P>

간단하게 이야기 하자면 [TYPE_A][TYPE_B] 로 변경하는 것이 목적입니다.

자바에서는 replaceAll 메소드와 정규식을 이용하면 쉽게 변환이 가능합니다.


우선 replaceAll 의 첫번째 파라미터를 설명하겠습니다.
'class='로 시작을 하고 스페이스, '>', 큰 따옴표가 나오기 전까지의 모든 문자들의 연속을 찾습니다. 괄호로 묶음은 그룹을 뜻하는데 여기서 그룹 1 은 'class=' 이며, 그룹 2 는 위의 [TYPE_A] 에서 라면 'PStyle0' 까지 입니다.

두번째 파라미터는 간단합니다. '$그룹번호' 로 변경하겠다는 이야기 입니다.
'$1\"$2\"' 을 단계별로 변환하면 그룹 1 이 교체되면 'class=\"$2\"' 가 되고 그룹 2 가 교체되면 'class=\"PStyle0\"' 이 됩니다.

정규식은 역시 활용만 잘하면 단순 노가다 작업을 간단하게 줄여줄 수 있는 강력한 무기가 됩니다.


Posted by 자수씨
, |

외부에서 받은 소스에 아래와 같이 불필요한 공백이 많다면...
한 페이지에 소스들이 안들어와서 개발에 힘이 듭니다.



500라인 정도라면 수작업으로 지울 수 있으나... 4000라인 정도라면 엄두조차 안나겠죠...

그래서 정규식을 조금 이용해 보았습니다.



정규식을 체크하시고 ^#R 로 검색조건을 걸고 대체어를 공백으로 하신 후 Replace All 버튼을 누르시면
불필요한 뉴라인은 사라지게 됩니다.

역시 강력한 규식이형!!!

Posted by 자수씨
, |

회사 프로젝트 코드에서 공백이 포함된 문자열을 공백을 제거할 로직을 추가할 일이 있어서
아래와 같은 방식으로 수정을 하였습니다.


허나 결과는...
Vicki Yi : Vicki Yi

정규식에서 공백은 정확하게 공백으로 인식을 못하는 것 같았습니다.
Pattern JavaDoc 에서 내용을 확인하였습니다.
http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html

POSIX character classes (US-ASCII only)
\p{Lower} A lower-case alphabetic character: [a-z]
\p{Upper} An upper-case alphabetic character:[A-Z]
\p{ASCII} All ASCII:[\x00-\x7F]
\p{Alpha} An alphabetic character:[\p{Lower}\p{Upper}]
\p{Digit} A decimal digit: [0-9]
\p{Alnum} An alphanumeric character:[\p{Alpha}\p{Digit}]
\p{Punct} Punctuation: One of !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
\p{Graph} A visible character: [\p{Alnum}\p{Punct}]
\p{Print} A printable character: [\p{Graph}\x20]
\p{Blank} A space or a tab: [ \t]
\p{Cntrl} A control character: [\x00-\x1F\x7F]
\p{XDigit} A hexadecimal digit: [0-9a-fA-F]
\p{Space} A whitespace character: [ \t\n\x0B\f\r]


그래서 아래와 같이 수정하였습니다.


결과는~~~ 원하는 결과가 나왔습니다.
Vicki Yi : VickiYi


정규식을 안쓰면 indexOf() 와 replace 를 while 문으로 돌려서 처리를 해야 하기에 역시 정규식이 깔끔한 것 같습니다.


Posted by 자수씨
, |

글 보관함

최근에 받은 트랙백