[JAVA] RegExp (정규 표현식)

Posted at 2009/07/01 21:45 // in 웹프로그래밍™/JSP,JAVA // by 블루비

<%
import!
java.util.regex.*; // Pattern과 Matcher가 속한 패키지

class RegularEx5
{
public static void main(String[] args)
{
String source = "23123()asdawe()23123()asdawe()23123()asdawe()";
String pattern = "\\d+\\(\\)";// 숫자(\\d)뒤에오는 괄호를 패턴으로 지정
// \d는 숫자를 의미하며 +는 하나이상의 숫자가 있어야함을 의미
// 괄호는 예약문자(?)이므로 \\를 앞에 붙여줘야 괄호로 인식
// 괄호앞에 \\를 붙이지 않으면 그룹화문자로 인식한다.
// \를 \\와 같이 두개씩 쓰는 이유는
//문자열 내에서 \를 표현하려면 두개 써줘야하기 때문


StringBuffer sb = new StringBuffer();

Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(source);

System.out.println("source:"+source);

int i=0;
while(m.find()) {

// 지정한 패턴과 매칭되는 부분을 치환한다. 마지막 두글자인 괄호를 잘라내고
// 원하는 문자(@@)로 바꾼다.
// find()를 통해서 패턴과 일치하는 부분은 m.group()을 통해서 얻는다.
m.appendReplacement(sb, m.group().substring(0, m.group().length()-2)+"@@");
}//end of while

m.appendTail(sb);
System.out.println("result:"+sb.toString());
}
}

/*
source:23123()asdawe()23123()asdawe()23123()asdawe()
result:23123@@asdawe()23123@@asdawe()23123@@asdawe()
*/

%>



숫자뒤에오는 ()를 @@로 바꾼겁니다.


ex)정규식 사용예

package test.regEx;

import! java.util.regex.*; // Pattern과 Matcher가 속한 패키지

class RegularEx {
public static void main(String[] args) {
String[] data = { "bat", "baby", "bonus", "c", "cA", "co", "c.",
"c0", "c#", "car", "combat", "count", "date", "disc", "mmcard", "mmca",
"ca", "ca&&", "^ca", "*ca~", "#*&ca)^(", "yraa*ca#iiop", "yraa*ca&", "yraa*ca", "ca^7ae", ")ca^7ae", "aa''ca%", "'(ca%^bc", "vv'(ca%^bc",
"dca~", "*ca0", "*ca0^", "%*ca0", "%*ca0ca^^", "%*ca가^^", "%*ㄱca^^",
"^@", "&*@~", "@_~", "@@", "#@@*", "*@@", "aa*@@", "yraa*@@(fa", "*@@0", "@@0", "aa''@%", "aa''@%(aa", "''@%(aa", "'@%(aa",
"**@rd", "mm@^^", "*@0*", "*@0@*", "*@ㄱ@*", "*@ㄱ*", "ㄱ@*"};
String[] pattern = { ".*", "c[a-z]*", "c[a-z]", "c[a-zA-Z]",
"c[a-zA-Z0-9]", "c.", "c.*", "c\\.", "c\\w", "c\\d", "c.*t",
"[b|c].*", ".*a.*", ".*a.+", "[b|c].{2}","[a-z]*ca[a-z]*",
"[^a-zA-Z0-9ㄱ-힣]*@[^a-zA-Z0-9ㄱ-힣]*", "@[^a-zA-Z0-9ㄱ-힣].*", "[a-zA-Z0-9ㄱ-힣]*[^a-zA-Z0-9ㄱ-힣]@", "[^a-zA-Z0-9ㄱ-힣]*@[^a-zA-Z0-9ㄱ-힣].*", "[a-zA-Z0-9ㄱ-힣]*[^a-zA-Z0-9ㄱ-힣]@[^a-zA-Z0-9ㄱ-힣].*",
"[^a-zA-Z0-9ㄱ-힣]*ca[^a-zA-Z0-9ㄱ-힣]*", "ca[^a-zA-Z0-9ㄱ-힣].*", "[a-zA-Z0-9ㄱ-힣]*[^a-zA-Z0-9ㄱ-힣]ca", "[^a-zA-Z0-9ㄱ-힣]*ca[^a-zA-Z0-9ㄱ-힣].*", "[a-zA-Z0-9ㄱ-힣].*[^a-zA-Z0-9ㄱ-힣]ca[^a-zA-Z0-9ㄱ-힣].*"};

for (int x = 0; x < pattern.length; x++) {
Pattern p = Pattern.compile(pattern[x]);
System.out.print("Pattern : " + pattern[x] + " 결과: ");
for (int i = 0; i < data.length; i++) {
Matcher m = p.matcher(data[i]);
if (m.matches())
System.out.print(data[i] + ", ");
}
System.out.println();
}
} // public static void main(String[] args)
}

/*

Pattern : .* 결과: bat, baby, bonus, c, cA, co, c., c0, c#, car, combat, count, date, disc, mmcard, mmca, ca, ca&&, ^ca, *ca~, #*&ca)^(, yraa*ca#iiop, yraa*ca&, yraa*ca, ca^7ae, )ca^7ae, aa''ca%, '(ca%^bc, vv'(ca%^bc, dca~, *ca0, *ca0^, %*ca0, %*ca0ca^^, %*ca가^^, %*ㄱca^^, ^@, &*@~, @_~, @@, #@@*, *@@, aa*@@, yraa*@@(fa, *@@0, @@0, aa''@%, aa''@%(aa, ''@%(aa, '@%(aa, **@rd, mm@^^, *@0*, *@0@*, *@ㄱ@*, *@ㄱ*, ㄱ@*,
Pattern : c[a-z]* 결과: c, co, car, combat, count, ca,
Pattern : c[a-z] 결과: co, ca,
Pattern : c[a-zA-Z] 결과: cA, co, ca,
Pattern : c[a-zA-Z0-9] 결과: cA, co, c0, ca,
Pattern : c. 결과: cA, co, c., c0, c#, ca,
Pattern : c.* 결과: c, cA, co, c., c0, c#, car, combat, count, ca, ca&&, ca^7ae,
Pattern : c\. 결과: c.,
Pattern : c\w 결과: cA, co, c0, ca,
Pattern : c\d 결과: c0,
Pattern : c.*t 결과: combat, count,
Pattern : [b|c].* 결과: bat, baby, bonus, c, cA, co, c., c0, c#, car, combat, count, ca, ca&&, ca^7ae,
Pattern : .*a.* 결과: bat, baby, car, combat, date, mmcard, mmca, ca, ca&&, ^ca, *ca~, #*&ca)^(, yraa*ca#iiop, yraa*ca&, yraa*ca, ca^7ae, )ca^7ae, aa''ca%, '(ca%^bc, vv'(ca%^bc, dca~, *ca0, *ca0^, %*ca0, %*ca0ca^^, %*ca가^^, %*ㄱca^^, aa*@@, yraa*@@(fa, aa''@%, aa''@%(aa, ''@%(aa, '@%(aa,
Pattern : .*a.+ 결과: bat, baby, car, combat, date, mmcard, ca&&, *ca~, #*&ca)^(, yraa*ca#iiop, yraa*ca&, yraa*ca, ca^7ae, )ca^7ae, aa''ca%, '(ca%^bc, vv'(ca%^bc, dca~, *ca0, *ca0^, %*ca0, %*ca0ca^^, %*ca가^^, %*ㄱca^^, aa*@@, yraa*@@(fa, aa''@%, aa''@%(aa, ''@%(aa, '@%(aa,
Pattern : [b|c].{2} 결과: bat, car,
Pattern : [a-z]*ca[a-z]* 결과: car, mmcard, mmca, ca,
Pattern : [^a-zA-Z0-9ㄱ-힣]*@[^a-zA-Z0-9ㄱ-힣]* 결과: ^@, &*@~, @_~, @@, #@@*, *@@,
Pattern : @[^a-zA-Z0-9ㄱ-힣].* 결과: @_~, @@, @@0,
Pattern : [a-zA-Z0-9ㄱ-힣]*[^a-zA-Z0-9ㄱ-힣]@ 결과: ^@, @@,
Pattern : [^a-zA-Z0-9ㄱ-힣]*@[^a-zA-Z0-9ㄱ-힣].* 결과: &*@~, @_~, @@, #@@*, *@@, *@@0, @@0, ''@%(aa, '@%(aa,
Pattern : [a-zA-Z0-9ㄱ-힣]*[^a-zA-Z0-9ㄱ-힣]@[^a-zA-Z0-9ㄱ-힣].* 결과: #@@*, *@@, aa*@@, yraa*@@(fa, *@@0, '@%(aa,
Pattern : [^a-zA-Z0-9ㄱ-힣]*ca[^a-zA-Z0-9ㄱ-힣]* 결과: ca, ca&&, ^ca, *ca~, #*&ca)^(,
Pattern : ca[^a-zA-Z0-9ㄱ-힣].* 결과: ca&&, ca^7ae,
Pattern : [a-zA-Z0-9ㄱ-힣]*[^a-zA-Z0-9ㄱ-힣]ca 결과: ^ca, yraa*ca,
Pattern : [^a-zA-Z0-9ㄱ-힣]*ca[^a-zA-Z0-9ㄱ-힣].* 결과: ca&&, *ca~, #*&ca)^(, ca^7ae, )ca^7ae, '(ca%^bc,
Pattern : [a-zA-Z0-9ㄱ-힣].*[^a-zA-Z0-9ㄱ-힣]ca[^a-zA-Z0-9ㄱ-힣].* 결과: yraa*ca#iiop, yraa*ca&, aa''ca%, vv'(ca%^bc,

*/


[참고]쌍따옴표(")내에서 escape문자(\)를 표현하려면 escape문자를 '\'와 같이 두 번 사용해야한다.


자주 쓰일 만한 몇 가지 패턴들을 만들어서 테스트하였다. 그 결과를 정리해보면 다음과 같다.


pattern0 = "[^가-힣]" '한글만
pattern1 = "[^-0-9 ]" '숫자만
pattern2 = "[^-a-zA-Z]" '영어만
pattern3 = "[^-가-힣a-zA-Z0-9/ ]" '숫자와 영어 한글만
pattern4 = "<[^>]*>" '태그만

pattern5 = "[^-a-zA-Z0-9/ ]" '영어 숫자만


정규식

설명

결과

c[a-z]*

c로 시작하는 영단어

c,ca,co,car,combat,count,

c[a-z]

c로 시작하는 두 자리 영단어

ca,co,

c[a-zA-Z]

c로 시작하는 두 자리 영단어

(a~z 또는 A~Z, 즉 대소문자 구분안함)

cA,ca,co,

c[a-zA-Z0-9]

c\w

c로 시작하고 숫자와 영어로 조합된 두 글자

cA,ca,co,c0,

.*

모든 문자열

bat,baby,bonus,c,cA,ca,co,c.,c0,c#,car,combat,count,date,disc,

c.

c로 시작하는 두 자리

cA,ca,co,c.,c0,c#,

c.*

c로 시작하는 모든 문자열(기호포함)

cA,ca,co,c.,c0,c#,car,combat,count,

c\.

c.와 일치하는 문자열

'.'은 정규식표현에 사용되는 문자이므로 escape문자인 ''를 사용해야한다.

c.,

c\d

c[0-9]

c와 숫자로 구성된 두 자리 문자열

c0,

c.*t

c로 시작하고 t로 끝나는 모든 문자열

combat,count,

[b|c].*

[bc].*

[b-c].*

b 또는 c로 시작하는 문자열

bat,baby,bonus,c,cA,ca,co,c.,c0,c#,car,combat,count,

[^b|c].*

[^bc].*

[^b-c].*

b 또는 c로 시작하지 않는 문자열

date,disc,

.*a.*

a를 포함하는 모든 문자열

* : 0 또는 그 이상의 문자

bat,baby,ca,car,combat,date,

.*a.+

a를 포함하는 모든 문자열.

+: 1 또는 그 이상의 문자

+는 *과는 달리 반드시 하나 이상의 문자가 있어야 하므로 a로 끝나는 단어는 포함되지 않았다.

bat,baby,car,combat,date,

[b|c].{2}

b 또는 c로 시작하는 세 자리 문자열.

(b 또는 c 다음에 두 자리이므로 모두 세 자리)

bat,car,

2009/07/01 21:45 2009/07/01 21:45

댓글을 남겨주세요.

[로그인][오픈아이디란?]

[JSP] web.xml - <error-page> 에러에 페이지 설정

Posted at 2009/06/30 20:44 // in 웹프로그래밍™/JSP,JAVA // by 블루비
JSP 에서 HTTP 에러 페이지를 설정을 위한 설정 입니다.

/WEB-INF/web.xml 파일에 다음과 같이 추가

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>http://blueb.net/blog</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>

<error-page>
<error-code>404</error-code>
<location>/error/code404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error/code500.jsp</location>
</error-page>

</web-app>




code404.jsp , code500.jsp 페이지를 /error 디렉토리에서 생성합니다.


code404.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
response.setStatus(HttpServletResponse.SC_OK);
%>
<html>
<head>
<title>404 Not Found</title>
</head>
<body>The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address. This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable. </body>
</html>


code500.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
response.setStatus(HttpServletResponse.SC_OK);
%>
<html>
<head>
<title>500 Internal Server Error</title>
</head>
<body>
The server encountered an unexpected condition which prevented it from fulfilling the request.
</body>
</html>


셋팅이 끝났다면

WAS 엔진을 제 가동합니다.

2009/06/30 20:44 2009/06/30 20:44

댓글을 남겨주세요.

[로그인][오픈아이디란?]

언어별 Redirect

Posted at 2009/06/30 20:37 // in 웹프로그래밍™/JSP,JAVA // by 블루비
언어별 Redirect

JSP
<% response.sendRedirect("http://blueb.net/blog"); %>

ASP
<% Response.Redirect "http://blueb.net/blog" %>

PHP
<? header("Location: http://blueb.net/blog"); exit; ?>

HTML
<meta http-equiv='refresh' content='0;url=http://blueb.net/blog'>

Javascript

<script type="text/javascript">
window.location.href = 'http://blueb.net/blog'
</script>

2009/06/30 20:37 2009/06/30 20:37

댓글을 남겨주세요.

[로그인][오픈아이디란?]

XSS (Cross Site Scription) 다양한 스크립트 유형

Posted at 2009/03/31 16:57 // in 웹프로그래밍™/JSP,JAVA // by 블루비
XSS 다양한 스크립트 유형

출처 : http://ha.ckers.org/xss.html

XSS more..

2009/03/31 16:57 2009/03/31 16:57

댓글을 남겨주세요.

[로그인][오픈아이디란?]