sitelink1 | http://blog.naver.com/chahojun?Redirect=...m/cuteshim |
---|---|
sitelink2 | |
sitelink3 | |
sitelink4 | |
sitelink5 | |
sitelink6 |
1. 기본예제
결과값 : match
·미리보기 | 소스복사·
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- * 정규표현식 기본 사용 예제
- *
- * @author Sehwan Noh <sehnoh@gmail.com>
- * @version 1.0 - 2006. 08. 22
- * @since JDK 1.4
- */
- public class RegExTest01 {
- public static void main(String[] args) {
- Pattern p = Pattern.compile("a*b");
- Matcher m = p.matcher("aaaaab");
- boolean b = m.matches();
- if (b) {
- System.out.println("match");
- } else {
- System.out.println("not match");
- }
- }
- }
2. 문자열 치환하기결과값 : one dog two dogs in the yard·미리보기 | 소스복사·
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- * 문자열 치환 예제
- *
- * @author Sehwan Noh <sehnoh@gmail.com>
- * @version 1.0 - 2006. 08. 22
- * @since JDK 1.4
- */
- public class RegExTest02 {
- public static void main(String[] args) {
- Pattern p = Pattern.compile("cat");
- Matcher m = p.matcher("one cat two cats in the yard");
- StringBuffer sb = new StringBuffer();
- while (m.find()) {
- m.appendReplacement(sb, "dog");
- }
- m.appendTail(sb);
- System.out.println(sb.toString());
- // or
- //String str = m.replaceAll("dog");
- //System.out.println(str);
- }
- }
3. 이메일 주소 유효검사결과값 : test@abc.com·미리보기 | 소스복사·
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- * 이메일주소 유효검사
- *
- * @author Sehwan Noh <sehnoh@gmail.com>
- * @version 1.0 - 2006. 08. 22
- * @since JDK 1.4
- */
- public class RegExTest03 {
- public static boolean isValidEmail(String email) {
- Pattern p = Pattern.compile("^(?:w+.?)*w+@(?:w+.)+w+$");
- Matcher m = p.matcher(email);
- return m.matches();
- }
- public static void main(String[] args) {
- String[] emails = { "test@abc.com", "a@.com", "abc@mydomain" };
- for (int i = 0; i < emails.length; i ++) {
- if (isValidEmail(emails[i])) {
- System.out.println(emails[i]);
- }
- }
- }
- }
4. HTML 태그 제거결과값 : Java2go.net Sehwan@Noh's Personal Workspace...·미리보기 | 소스복사·
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- * HTML 태그 제거
- *
- * @author Sehwan Noh <sehnoh@gmail.com>
- * @version 1.0 - 2006. 08. 22
- * @since JDK 1.4
- */
- public class RegExTest04 {
- public static String stripHTML(String htmlStr) {
- Pattern p = Pattern.compile("<(?:.|s)*?>");
- Matcher m = p.matcher(htmlStr);
- return m.replaceAll("");
- }
- public static void main(String[] args) {
- String htmlStr = "<html><body><h1>Java2go.net</h1>"
- + " <p>Sehwan@Noh's Personal Workspace...</p></body></html>";
- System.out.println(stripHTML(htmlStr));
- }
- }
5. HTML 링크 만들기결과값 : My homepage URL is <a href='http://www.java2go.net/index.html'>http://www.java2go.net/index.html</a>.·미리보기 | 소스복사·
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- * HTML 링크 만들기
- *
- * @author Sehwan Noh <sehnoh@gmail.com>
- * @version 1.0 - 2006. 08. 22
- * @since JDK 1.4
- */
- public class RegExTest05 {
- public static String linkedText(String sText) {
- Pattern p = Pattern.compile(
- "(http|https|ftp)://[^s^.]+(.[^s^.]+)*");
- Matcher m = p.matcher(sText);
- StringBuffer sb = new StringBuffer();
- while (m.find()) {
- m.appendReplacement(sb,
- "<a href='" + m.group()+"'>" + m.group() + "</a>");
- }
- m.appendTail(sb);
- return sb.toString();
- }
- public static void main(String[] args) {
- String strText =
- "My homepage URL is http://www.java2go.net/home/index.html.";
- System.out.println(linkedText(strText));
- }
- }
6. 금지어 필터링 하기·미리보기 | 소스복사·
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- * 금지어 필터링하기
- *
- * @author Sehwan Noh <sehnoh@gmail.com>
- * @version 1.0 - 2006. 08. 22
- * @since JDK 1.4
- */
- public class RegExTest06 {
- public static String filterText(String sText) {
- Pattern p = Pattern.compile("fuck|shit|개새끼", Pattern.CASE_INSENSITIVE);
- Matcher m = p.matcher(sText);
- StringBuffer sb = new StringBuffer();
- while (m.find()) {
- //System.out.println(m.group());
- m.appendReplacement(sb, maskWord(m.group()));
- }
- m.appendTail(sb);
- //System.out.println(sb.toString());
- return sb.toString();
- }
- public static String maskWord(String word) {
- StringBuffer buff = new StringBuffer();
- char[] ch = word.toCharArray();
- for (int i = 0; i < ch.length; i++) {
- if (i < 1) {
- buff.append(ch[i]);
- } else {
- buff.append("*");
- }
- }
- return buff.toString();
- }
- public static void main(String[] args) {
- String sText = "Shit! Read the fucking manual. 개새끼야.";
- System.out.println(filterText(sText));
- }
- }
결과값 : S***! Read the f***ing manual. 개**야.