본문 바로가기

기타

정규식 정리

javascript에서 사용하는 PCRE정규식 정리문서

이스케이프 문자

단순 이스케이프 문자

  • \n: new line(ASCII: 0x0A)
  • \f: formfeed(ASCII: 0x0C)
  • \r: carrage return (ASCII: 0x0D)
  • \aaa: 유니코드의 8진수 aaa에 매칭되는 문자
  • \xFF: 1바이트의 유니코드 표현
  • \uFFFF: 2바이트의 유니코드 표현
  • \u{FFFF}: 2바이트 이상의 유니코드 표현
  • \040: 공백(8진수 표현)
  • \0: null

만약 검색이 안될 시 유니코드 표시를 이용하면 된다

사용 방법(language: javascript)

const regex1=/[\x61\u{1F60A}]/gu;
const regex2=/[\n\f\r]/g;
const regex3=/\040/g;
const string=`asdf \nzxcv\fqwer\rqwer😊`;

console.log(string.match(regex1)); // [ 'a', '😊' ]
console.log(string.match(regex2)); // [ '\n', '\f', '\r' ]
console.log(string.match(regex3)); // [ ' ' ]

범위를 나타내는 이스케이프 문자

정규식에만 존재하는 이스케이프 문자이다.

  • \d: 0,1,2,3,4등의 숫자1개를 표현
  • \w: 0,1,2,a,b,c,A,B,C등의 특수문자를 제외한 모든 문자 1개를 표현. 단 _ 문자는 특수문자 취급하지 않은다.
  • \s: 공백 문자
  • \S: 공백을 제외한 모든 문자
  • \D: 숫자를 제외한 모든 문자
  • \W: 특수문자, 공백 등 \w에 포함되지 않은 모든 문자

사용 방법(language: javascript)

const regex1 = /\d/g;
const regex2 = /\w/g;
const regex3 = /\s/g;
const regex4 = /\S/g;
const regex5 = /\D/g;
const regex6 = /\W/g;
const string = '1Q@w3E_- \n';

console.log(string.match(regex1)); // [ '1', '3' ]
console.log(string.match(regex2)); // [ '1', 'Q', 'w', '3', 'E', '_' ]
console.log(string.match(regex3)); // [ ' ', '\n' ]
console.log(string.match(regex4)); // [ '1', 'Q', '@', 'w', '3', 'E', '_', '-' ]
console.log(string.match(regex5)); // [ 'Q', '@',  'w', 'E', '_',  '-', ' ', '\n' ]
console.log(string.match(regex6)); // [ '@', '-', ' ', '\n' ]

메타 문자

  • .: 개행을 제외한 모든 문자 1개
  • (): 서브패턴
  • -: 문자 범위
  • $: 문장의 시작
  • ^문장의 끝
  • ?<name>: 서브패턴 이름

사용 방법(language: javascript)

const string1='test1 test2';

const regex1=/./g
const regex2=/(?<t_char>t).(s)/
const regex3=/[a-t]+/
const regex4=/^t/
const regex5=/\d$/
const regex6=/^t[a-z]{2,}/
const regex7=/[a-z]{2,}\d$/

console.log(string1.match(regex1)); // ['t', 'e', 's', 't', '1', ' ', 't', 'e', 's', 't', '2']
console.log(string1.match(regex2)); // ['tes', 't', 's', index: 0, input: 'test1 test2', groups: {t_char: 't'}]
console.log(string1.match(regex3)); // ['test', index: 0, input: 'test1 test2', groups: undefined]
console.log(string1.match(regex4)); // ['t', index: 0, input: 'test1 test2', groups: undefined]
console.log(string1.match(regex5)); // ['2', index: 10, input: 'test1 test2', groups: undefined]
console.log(string1.match(regex6)); // ['test', index: 0, input: 'test1 test2', groups: undefined]
console.log(string1.match(regex7)); // ['test2', index: 6, input: 'test1 test2', groups: undefined]

반복 탐색

  • +: 앞의 문자가 1개 이상 반복된 문자
  • *: 앞의 문자가 0개 이상 반복된 문자
  • {1}: 앞의 문자가 1번만 반복된 문자
  • {1,3}: 앞의 문자가 최소1번, 최대 3번 반복된 문자
  • {1,}: 앞의 문자가 1개 이상 반복된 문자(=+)

사용 방법(language: javascript)

const string1='a advise acquire apologize'

const regex1=/a/g
const regex2=/a\w+/g
const regex3=/a\w*/g
const regex4=/a\w{5}/g
const regex5=/a\w{0,5}/g
const regex6=/a\w{7,}/g

console.log(string1.match(regex1)); // ['a', 'a', 'a', 'a']
console.log(string1.match(regex2)); // ['advise', 'acquire', 'apologize']
console.log(string1.match(regex3)); // ['a', 'advise', 'acquire', 'apologize']
console.log(string1.match(regex4)); // ['advise', 'acquir', 'apolog']
console.log(string1.match(regex5)); // ['a', 'advise', 'acquir', 'apolog']
console.log(string1.match(regex6)); // ['apologize']

범위 지정

  • [0-9]: 0부터 9까지의 숫자 중 하나
  • [az]: 문자 a,z중 하나
  • [0-9a-zA-Z]: 숫자, 대소문자 구분없는 알파벳 문자 중 하나

사용 방법(language: javascript)

const string1='0:zero 1:One 2:Two'

// const string1='test1 test2';

const regex1=/[0-9]/g
const regex2=/0:[az]/g
const regex3=/[0-9]:[0-9a-zA-Z]+/g

console.log(string1.match(regex1)); // ['0', '1', '2']
console.log(string1.match(regex2)); // ['0:z']
console.log(string1.match(regex3)); // ['0:zero', '1:One', '2:Two']

존재 여부

  • ?: 문자가 있거나 없거나(or)
  • |: 앞의 패턴 혹은 뒤의 패턴 중 하나

사용 방법(language: javascript)

const string1='true|false'
const string2='false|true'

const regex1=/t?\w*/
const regex2=/(t|f)\w*/

console.log(string1.match(regex1)); // ['true', index: 0, input: 'true|false', groups: undefined]
console.log(string1.match(regex2)); // ['true', 't', index: 0, input: 'true|false', groups: undefined]
console.log(string2.match(regex1)); // ['false', index: 0, input: 'false|true', groups: undefined]
console.log(string2.match(regex2)); // ['false', 'f', index: 0, input: 'false|true', groups: undefined]

? Syntax는 존재여부로 사용할 수 있고 lazy로사용할 수 있다. lazy는 반복패턴에서 사용되는 것으로서 반복패턴이 최대로 만족하는 문자를 찾도록 하는 문법이다.

'기타' 카테고리의 다른 글

[TDD] 좋은 테스트를 작성하기 위한 원칙들  (0) 2023.02.21
정규식 정리2  (0) 2021.10.20