티스토리 뷰

예제 코드는 모두 Java 언어를 사용한다.

문자 그대로 찾기

문자 그대로 찾으려면 찾고 싶은 문자열 그대로 쓰면 된다.

  • regex : 찾고 싶은 문자열
  • str : 해당 문자열
public class code {
    public static void main(String[] args) {
        String regex = "Eric";

        String str = "Hello, my name is Eric. Please visit " +
                "my github at <https://github.com/kkoon9>";

        if(str.contains(regex)) {
            System.out.println("str 안에 " + regex + " 문자열이 존재한다.");
        } else {
            System.out.println("str 안에 " + regex + " 문자열이 존재하지 않는다.");
        }
    }
}
// 실행결과 : str 안에 Eric 문자열이 존재한다.

Java같은 경우는 대소문자 구별을 무시하지 않는다.

public class code {
    public static void main(String[] args) {
        String regex = "eric";

        String str = "Hello, my name is Eric. Please visit " +
                "my github at <https://github.com/kkoon9>";

        if(str.contains(regex)) {
            System.out.println("str 안에 " + regex + " 문자열이 존재한다.");
        } else {
            System.out.println("str 안에 " + regex + " 문자열이 존재하지 않는다.");
        }
    }
}
// 실행결과 : str 안에 Eric 문자열이 존재하지 않는다.

모든 문자 찾기

위 같은 경우는 정적 텍스트를 찾는 방법이었다.

정규 표현식에서는 특별한 문자들을 써 무엇을 검색할지 결정할 수 있다.

마침표(.) 문자는 아무 문자 하나와 일치한다.

따라서 e.ic를 검색하면 eric, ekic, epic를 포함하여 많은 단어들과 일치하게 된다.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class code {
    public static void main(String[] args) {
        String regex = "e.ic";
        Pattern pattern = Pattern.compile(regex);

        String[] strs = {
                "eric",
                "ekicb",
                "e.icz",
                "erik"
        };

        for(String str : strs) {
            Matcher matcher = pattern.matcher(str);
            if (matcher.find()) {
                System.out.println("str 안에 " + regex + " 문자열이 존재한다.");
            } else {
                System.out.println("str 안에 " + regex + " 문자열이 존재하지 않는다.");
            }
        }
    }
}
/* 실행결과
str 안에 e.ic 문자열이 존재한다.
str 안에 e.ic 문자열이 존재한다.
str 안에 e.ic 문자열이 존재한다.
str 안에 e.ic 문자열이 존재하지 않는다.
*/

Java에서는 Matcher, Pattern 클래스로 정규표현식을 사용할 수 있다.

마침표(.) 문자는 어떠한 문자나 알파벳, 숫자 심지어는 문장 부호로 쓰인 마침표(.) 자체와도 일치한다.

마침표(.) 문자 여러 개를 동시에 사용할 수도 있다.

..처럼 연속해 사용하면 어떠한 문자든 붙어있는 문자 두 개와 일치한다.

아니면 서로 다른 위치에 사용해도 된다.

public class code {
    public static void main(String[] args) {
        String regex = "e..c.";
        Pattern pattern = Pattern.compile(regex);

        String[] strs = {
                "eric",
                "ekicz",
                "e1icz",
                "erikz"
        };

        for(String str : strs) {
            Matcher matcher = pattern.matcher(str);
            if (matcher.find()) {
                System.out.println("str 안에 " + regex + " 문자열이 존재한다.");
            } else {
                System.out.println("str 안에 " + regex + " 문자열이 존재하지 않는다.");
            }
        }
    }
}
/* 실행결과
str 안에 e..c. 문자열이 존재하지 않는다.
str 안에 e..c. 문자열이 존재한다.
str 안에 e..c. 문자열이 존재한다.
str 안에 e..c. 문자열이 존재하지 않는다.
*/

특수문자 찾기

❓ 만약에 진짜 마침표(.) 문자를 찾고 싶을 때에는 어떻게 해야 할까?

 

 

이 때 역슬래시(\) 문자를 사용하면 된다.

역슬래시(\)는 메타 문자라고 칭한다.

🐻 메타 문자는 문자 그대로 사용되지 않고 특별한 의미를 지니는 문자를 뜻한다.

 

Java에서는 역슬래시(\)를 쓰기 위해서는 역슬래시를 두 번 써줘야 한다.

public class code {
    public static void main(String[] args) {
        String regex = "e.ic.\\\\.";
        Pattern pattern = Pattern.compile(regex);

        String[] strs = {
                "ericz.txt",
                "ekicz.html",
                "e1icz",
                "eri.k"
        };

        for(String str : strs) {
            Matcher matcher = pattern.matcher(str);
            if (matcher.find()) {
                System.out.println("str 안에 " + regex + " 문자열이 존재한다.");
            } else {
                System.out.println("str 안에 " + regex + " 문자열이 존재하지 않는다.");
            }
        }
    }
}
/* 실행결과
str 안에 e.ic.\\. 문자열이 존재한다.
str 안에 e.ic.\\. 문자열이 존재한다.
str 안에 e.ic.\\. 문자열이 존재하지 않는다.
str 안에 e.ic.\\. 문자열이 존재하지 않는다.
*/
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
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
글 보관함