정규 표현식 Regular expressions (Regex)

    Java regex 공식문서

     

    .matches()

    String pattern = "\\d";
    boolean isNum = "1".matches(pattern); //true

    자바에서는 '\' 를 '\\'로 표현해야 함

     

     

    Character classes

    [abc] a, b, or c (simple class)
    [^abc] Any character except a, b, or c (negation)
    [a-zA-Z] a through z or A through Z, inclusive (range)
    [a-d[m-p]] a through d, or m through p: [a-dm-p] (union)
    [a-z&&[def]] d, e, or f (intersection)
    [a-z&&[^bc]] a through z, except for b and c: [ad-z] (subtraction)
    [a-z&&[^m-p]] a through z, and not m through p: [a-lq-z](subtraction)

     

    Predefined character classes

    . Any character (may or may not match line terminators)
    \d A digit: [0-9]
    \D A non-digit: [^0-9]
    \s A whitespace character: [ \t\n\x0B\f\r]
    \S A non-whitespace character: [^\s]
    \v A vertical whitespace character: [\n\x0B\f\r\x85\u2028\u2029] \n: 줄바꿈, \t: tab
    \V A non-vertical whitespace character: [^\v]
    \w A word character: [a-zA-Z_0-9]
    \W A non-word character: [^\w]

     

    Boundary matchers

    ^ The beginning of a line
    $ The end of a line
    String temp = ".dfa,sd3-."
    temp = temp.replaceAll("^[.] | [.]$",""); //처음, 마지막에'.'있다면 '.' 빼기

     

    $ 는 문자열의 종료지점을 찾는다.따라서 $ 앞의 패턴으로 문자열이 끝나는 것을 찾는다.
    ^ 는 문자열의 시작지점을 찾sms다. 따라서 ^ 다음 패턴으로 문자열이 시작되는 것을 찾는다.

     

    Greedy quantifiers

    X? X, once or not at all
    X* X, zero or more times
    X+ X, one or more times
    X{n} X, exactly n times
    X{n,} X, at least n times
    X{n,m} X, at least n but not more than m times

     

    Reluctant quantifiers

    X?? X, once or not at all
    X*? X, zero or more times
    X+? X, one or more times
    X{n}? X, exactly n times
    X{n,}? X, at least n times
    X{n,m}? X, at least n but not more than m times

     

    Possessive quantifiers

    X?+ X, once or not at all
    X*+ X, zero or more times
    X++ X, one or more times
    X{n}+ X, exactly n times
    X{n,}+ X, at least n times
    X{n,m}+ X, at least n but not more than m times

     

    Greedy vs. Reluctant vs. Possessive Qualifiers 두번째 답변 그림

     

    // 연속된 '.' 하나의 '.'으로 변경
    String temp = "s.daf...sdf..df"
    temp = temp.replaceAll("[.]{2,}","");

     

    정규 표현식 참고 사이트 : https://codechacha.com/ko/java-regex/

     

    정규 표현식을 이용한 알고리즘 문제

    1. 신규 아이디 추천

    내 깃허브

    Programmers

    728x90

    댓글