Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| public:computer:regexp [2021/08/23 20:52] – alex | public:computer:regexp [2023/01/01 00:58] (current) – [선택, 그룹, 역참조] alex | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Regular Expressions ====== | ====== Regular Expressions ====== | ||
| - | <lead> | + | > **정규표현식은 특정 문자열을 가리키는 패턴이다. 즉 이 패턴으로 원하는 문자열을 찾는다.** - 켄 톰슨 |
| ===== 기본 패턴 찾기 ===== | ===== 기본 패턴 찾기 ===== | ||
| Line 9: | Line 9: | ||
| * 수량자; {숫자}: 해당 갯수, {최소숫자, | * 수량자; {숫자}: 해당 갯수, {최소숫자, | ||
| * 상수 | * 상수 | ||
| + | |||
| + | ^ 정규표현식의 메타 문자 | ||
| + | ^ 메타 문자 | ||
| + | ^ . | 점 | U+002E | ||
| + | ^ \ | 역슬래시 | ||
| + | ^ %%|%% | 세로줄 | ||
| + | ^ %%^%% | 악센트 기호 | ||
| + | ^ $ | 달러 기호 | ||
| + | ^ ? | 물음표 | ||
| + | ^ * | 별표 | ||
| + | ^ + | 덧셈 기호 | ||
| + | ^ [ | 왼쪽 대괄호 | ||
| + | ^ ] | 오른쪽 대괄호 | ||
| + | ^ { | 왼쪽 중괄호 | ||
| + | ^ } | 오른쪽 중괄호 | ||
| + | ^ ( | 왼쪽 소괄호 | ||
| + | ^ ) | 오른쪽 소괄호 | ||
| + | |||
| + | |||
| ==== 숫자 ==== | ==== 숫자 ==== | ||
| Line 19: | Line 38: | ||
| ^ 단축 문자((이 외에 더 있음, 정규표현식 엔진에 따라 지원 여부 다양)) | ^ 단축 문자((이 외에 더 있음, 정규표현식 엔진에 따라 지원 여부 다양)) | ||
| + | ^ 단축문자 | ||
| ^ \a | 벨 문자 | ^ \a | 벨 문자 | ||
| ^ [\b] | 백스페이스 문자 | ^ [\b] | 백스페이스 문자 | ||
| Line 50: | Line 70: | ||
| ==== 참조 그룹 ==== | ==== 참조 그룹 ==== | ||
| - | * 찾고자 하는 내용을 ()로 감싼 후(참조) $1, $2 혹은 \1, \2와 같이 역참조 | + | * 찾고자 하는 내용을 ()로 감싼 후(참조) |
| Line 80: | Line 100: | ||
| ^ 정규표현식 옵션((서브패턴 이름 지정은 http:// | ^ 정규표현식 옵션((서브패턴 이름 지정은 http:// | ||
| + | ^ 옵션 | ||
| ^ (?d) | 유닉스 행 | 자바 | ^ (?d) | 유닉스 행 | 자바 | ||
| ^ (?i) | 대소문자 구분 없앰 | ^ (?i) | 대소문자 구분 없앰 | ||
| Line 91: | Line 112: | ||
| ^ Perl 변경자(플래그)((http:// | ^ Perl 변경자(플래그)((http:// | ||
| + | ^ 변경자 | ||
| ^ a | \d, \s, \w 및 ASCII 범위 내의 POSIX 문자 | ^ a | \d, \s, \w 및 ASCII 범위 내의 POSIX 문자 | ||
| ^ c | 찾기 실패 후 현재 위치 유지 | ^ c | 찾기 실패 후 현재 위치 유지 | ||
| Line 104: | Line 126: | ||
| * 서브패턴 ex) (the|The|THE), | * 서브패턴 ex) (the|The|THE), | ||
| - | * 그룹 참조와 역참조; \1, $1 | + | * 그룹 참조와 역참조; \1, \$1 |
| * 그룹 이름 지정; ?< | * 그룹 이름 지정; ?< | ||
| - | * 그룹 이름으로 참조; $+{one}, $+{two} | + | * 그룹 이름으로 참조; |
| ^ 그룹 이름 지정 구문 | ^ 그룹 이름 지정 구문 | ||
| + | ^ 구문 | ||
| ^ (?< | ^ (?< | ||
| ^ (? | ^ (? | ||
| Line 131: | Line 154: | ||
| ==== POSIX 문자 클래스 ==== | ==== POSIX 문자 클래스 ==== | ||
| ^ POSIX 문자 클래스 | ^ POSIX 문자 클래스 | ||
| - | ^ [[: | + | ^ 문자 클래스 |
| - | ^ [[: | + | ^ < |
| - | ^ [[: | + | ^ < |
| - | ^ [[: | + | ^ < |
| - | ^ [[: | + | ^ < |
| - | ^ [[: | + | ^ < |
| - | ^ [[: | + | ^ < |
| - | ^ [[: | + | ^ < |
| - | ^ [[: | + | ^ < |
| - | ^ [[: | + | ^ < |
| - | ^ [[: | + | ^ < |
| - | ^ [[: | + | ^ < |
| - | ^ [[: | + | ^ < |
| - | ^ [[: | + | ^ < |
| + | ^ < | ||
| ===== 유니코드와 기타 문자 ===== | ===== 유니코드와 기타 문자 ===== | ||
| + | * \u16진수코드; | ||
| + | * 8진수로 문자 찾기; \351(==\u00e9) | ||
| + | * 제어문자 찾기; /cx | ||
| + | |||
| ===== 수량자 ===== | ===== 수량자 ===== | ||
| + | * 욕심많고, | ||
| + | * 욕심쟁이; | ||
| + | * 게으른(또는 느슨한) 수량자; 우선 문자열의 시작 위치부터 검색. 원하는 문자열을 찾을 때까지 하나씩 문자를 증가시키면서 찾다가, 계속 찾지 못하면 마지막으로 전체 텍스트를 검사. 일반 수량자 뒤에 물음표(? | ||
| + | * 독점적인 수량자; 전체 텍스트를 가져온 후, 찾고자 하는 문자열인지를 검사. 오직 한 번만 시도, 백트래킹은 하지 않음. 일반 수량자 뒤에 덧셈기호(+)를 붙인다. | ||
| + | * 클리니 스타(*) | ||
| + | * *, +, ? | ||
| + | |||
| + | ^ 기본 수량자 | ||
| + | ^ 구문 | ||
| + | ^ ? | 0 또는 하나 | ||
| + | ^ + | 하나 이상 | ||
| + | ^ * | 0 또는 그 이상 | ||
| + | |||
| + | ^ 게으른 수량자 | ||
| + | ^ 구문 | ||
| + | ^ ?? | 게으르게 0번 또는 한 번 | | ||
| + | ^ +? | 게으르게 한 번 이상 | ||
| + | ^ *? | 게으르게 0번 이상 | ||
| + | ^ {n}? | 게으르게 n번 | | ||
| + | ^ {n.}? | 게으르게 n번 이상 | ||
| + | ^ {m, | ||
| + | |||
| + | ^ 독정적인 수량자 | ||
| + | ^ 구문 | ||
| + | ^ ?+ | 독점적으로 0번 또는 한 번 | | ||
| + | ^ ++ | 독점적으로 한 번 이상 | ||
| + | ^ *+ | 독점적으로 0번 이상 | ||
| + | ^ {n}+ | 독점적으로 n번 | | ||
| + | ^ {n.}+ | 독점적으로 n번 이상 | ||
| + | ^ {m, | ||
| + | |||
| + | |||
| + | |||
| + | ===== 탐색 ===== | ||
| + | * lookaround | ||
| + | * 긍정형 전방 탐색(Positive lookaheads) | ||
| + | * 부정형 전방 탐색(Negative lookaheads) | ||
| + | * 긍정형 후방 탐색(Positive lookbehinds) | ||
| + | * 부정형 후방 탐색(Negative lookbehinds) | ||
| ===== 자주 쓰는 정규표현식 ===== | ===== 자주 쓰는 정규표현식 ===== | ||
| Line 187: | Line 254: | ||
| - -가 2개 이상 | - -가 2개 이상 | ||
| - >로 닫음 | - >로 닫음 | ||
| + | |||
| + | * < | ||
| + | * 숫자를 제외한 모든 문자 제거; <alert type=" | ||
| + | * 숫자만으로 되어있는 사업자 번호 -> 사업자번호 형식; <alert type=" | ||
| + | * 숫자만으로만 되어있는지 확인; <alert type=" | ||
| + | * 첫글자만 보이게 마스킹; <alert type=" | ||
| + | * Password Validataions (JAVA & TypeScript)< | ||
| + | |||
| + | public static Boolean hasSpecialCharacter(String strPassword) | ||
| + | { | ||
| + | Boolean bHasSpecialCharacter = Pattern.matches(" | ||
| + | |||
| + | if(true == bHasSpecialCharacter) | ||
| + | { | ||
| + | System.out.println(strPassword + " has Special Character." | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | System.out.println(strPassword + " has no Special Character." | ||
| + | } | ||
| + | |||
| + | return bHasSpecialCharacter; | ||
| + | } | ||
| + | |||
| + | public static Boolean isValidPassword(String strPassword) | ||
| + | { | ||
| + | Boolean bValid = Pattern.matches(" | ||
| + | |||
| + | System.out.println(strPassword + ":" | ||
| + | return bValid; | ||
| + | } | ||
| + | |||
| + | public static Boolean hasAlphabet(String strPassword) | ||
| + | { | ||
| + | Boolean bHasAlphabet = Pattern.matches(" | ||
| + | |||
| + | if(true == bHasAlphabet) | ||
| + | { | ||
| + | System.out.println(strPassword + " has Alphabet." | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | System.out.println(strPassword + " has no Alphabet." | ||
| + | } | ||
| + | |||
| + | return bHasAlphabet; | ||
| + | } | ||
| + | |||
| + | public static Boolean hasDigit(String strPassword) | ||
| + | { | ||
| + | Boolean bHasDigit = Pattern.matches(" | ||
| + | |||
| + | if(true == bHasDigit) | ||
| + | { | ||
| + | System.out.println(strPassword + " has Digits." | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | System.out.println(strPassword + " has no Digits." | ||
| + | } | ||
| + | |||
| + | return bHasDigit; | ||
| + | } | ||
| + | |||
| + | public static void testPassword(String strPassword) | ||
| + | { | ||
| + | System.out.println(" | ||
| + | |||
| + | hasAlphabet(strPassword); | ||
| + | hasDigit(strPassword); | ||
| + | hasSpecialCharacter(strPassword); | ||
| + | }</ | ||
| + | function hasAlphabet(strPassword: | ||
| + | const reg = new RegExp(" | ||
| + | const bHasAlphabet = reg.test(strPassword) | ||
| + | |||
| + | if(false === bHasAlphabet) { | ||
| + | console.log(`${strPassword} has no alphabet.`) | ||
| + | } else { | ||
| + | console.log(`${strPassword} has alphabet.`) | ||
| + | } | ||
| + | | ||
| + | return bHasAlphabet | ||
| + | } | ||
| + | |||
| + | function hasDigit(strPassword: | ||
| + | const reg = new RegExp(" | ||
| + | const bHasDigit = reg.test(strPassword) | ||
| + | |||
| + | if(false === bHasDigit) { | ||
| + | console.log(`${strPassword} has no digit.`) | ||
| + | } else { | ||
| + | console.log(`${strPassword} has digit.`) | ||
| + | } | ||
| + | |||
| + | return bHasDigit | ||
| + | } | ||
| + | |||
| + | function hasSpecialCharacter(strPassword: | ||
| + | const reg = new RegExp(" | ||
| + | const bHasSpecialCharacter = reg.test(strPassword) | ||
| + | |||
| + | if(false === bHasSpecialCharacter){ | ||
| + | console.log(`${strPassword} has no special character.`) | ||
| + | } else { | ||
| + | console.log(`${strPassword} has special character.`) | ||
| + | } | ||
| + | |||
| + | return bHasSpecialCharacter | ||
| + | } | ||
| + | |||
| + | function isValidPassword(strPassword: | ||
| + | const reg = new RegExp(" | ||
| + | const bValid = reg.test(strPassword) | ||
| + | |||
| + | if(false === bValid) { | ||
| + | console.log(`${strPassword} is invalid.`) | ||
| + | } else { | ||
| + | console.log(`${strPassword} is valid.`) | ||
| + | } | ||
| + | |||
| + | return bValid | ||
| + | } | ||
| + | |||
| + | function testPassword(strPassword: | ||
| + | console.log(`Your Password is : ${strPassword}`) | ||
| + | |||
| + | hasAlphabet(strPassword) | ||
| + | hasDigit(strPassword) | ||
| + | hasSpecialCharacter(strPassword) | ||
| + | |||
| + | isValidPassword(strPassword) | ||
| + | |||
| + | }</ | ||
| + | * camelCase to snake_case and MACRO_CASE (JAVA)< | ||
| + | public static String getSnakeCaseFromCamelCase(String strCamelCase) | ||
| + | { | ||
| + | return strCamelCase.replaceAll(" | ||
| + | } | ||
| + | |||
| + | public static String getMacroCaseFromCamelCase(String strCamelCase) | ||
| + | { | ||
| + | return strCamelCase.replaceAll(" | ||
| + | } | ||
| + | </ | ||
| + | * Card Number Validation (TypeScript)< | ||
| + | function validateIBKCEOCard(strCardNumber: | ||
| + | const regDigitsOnly = /(\D+)/gi | ||
| + | const strDigits = strCardNumber.replace(regDigitsOnly, | ||
| + | |||
| + | if (strDigits.length != 16) { | ||
| + | console.log(strCardNumber + ' is invalid card number' | ||
| + | return | ||
| + | } | ||
| + | | ||
| + | console.log(' | ||
| + | |||
| + | const regValidate: | ||
| + | const bValid = regValidate.test(strDigits) | ||
| + | |||
| + | console.log(' | ||
| + | } | ||
| + | </ | ||
| + | |||
| ===== Tools ===== | ===== Tools ===== | ||
| + | |||
| + | ==== 프로그램과 라이브러리 ==== | ||
| + | * Perl | ||
| + | * PCRE(Perl Compatible Regular Expression) | ||
| + | * 루비에서 사용하는 오니구루마 | ||
| + | * 파이썬 | ||
| + | * RE2 | ||
| + | |||
| + | |||
| ==== On-line ==== | ==== On-line ==== | ||
| * [[https:// | * [[https:// | ||
| * [[https:// | * [[https:// | ||
| - | * rubular | + | * [[https://rubular.com/ |
| Line 207: | Line 447: | ||
| * [[https:// | * [[https:// | ||
| * reggy | * reggy | ||
| + | |||
| + | |||
| + | ===== References ===== | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||