본문 바로가기

Programming/Javascript

Javascript 정규 표현식 (Regular Expression)

반응형

 정규 표현식(정규식)

: 특정한 규칙(패턴)을 가진 문자열의 집합을 표현하는 데 사용하는 형식 언어이다
 
 
정규식은 보통 다음과 같이 사용한다.
/패턴/플래그
'/(구분문자)' 사이에 매칭시킬 패턴을 작성한다.
'/패턴/' 다음에 옵션을 설정하는 플래그를 작성한다.
메타문자 설명 예시
. 일반적으로 새 줄을 제외한 모든 어떠한 문자열과도 일치한다.
$string1 = "Hello World\n";
if ($string1 =~ m/...../) {
  print "$string1 has length >= 5\n";
}
출력:
Hello World
 has length >= 5
( ) 일련의 패턴 요소들을 하나의 요소로 묶는다. 
괄호 안의 패턴을 일치시킬 때 $1, $2, ... 중 하나를 사용할 수 있다.
$string1 = "Hello World\n";
if ($string1 =~ m/(H..).(o..)/) {
  print "We matched '$1' and '$2'\n";
}
출력:
We matched 'Hel' and 'o W'
+ 1번 이상 발생하는 패턴과 일치시킨다.
$string1 = "Hello World\n";
if ($string1 =~ m/l+/) {
  print "There are one or more consecutive letter \"l\"'s in $string1\n";
}
출력:
There are one or more consecutive letter "l"'s in Hello World
? 0~1번 발생하는 패턴과 일치시킨다.
$string1 = "Hello World\n";
if ($string1 =~ m/H.?e/) {
  print "There is an 'H' and a 'e' separated by ";
  print "0-1 characters (Ex: He Hoe)\n";
}
출력:
There is an 'H' and a 'e' separated by 0-1 characters (Ex: He Hoe)
?  
$string1 = "Hello World\n";
if ($string1 =~ m/(l.+?o)/) {
  print "The non-greedy match with 'l' followed by one or\n";
  print "more characters is 'llo' rather than 'llo Wo'.\n";
}
출력:
The non-greedy match with 'l' followed by one or
more characters is 'llo' rather than 'llo Wo'.
* 0번 이상 발생하는 패턴과 일치시킨다.
$string1 = "Hello World\n";
if ($string1 =~ m/el*o/) {
  print "There is an 'e' followed by zero to many ";
  print "'l' followed by 'o' (eo, elo, ello, elllo)\n";
}
출력:
There is an 'e' followed by zero to many 'l' followed by 'o' (eo, elo, ello, elllo)
{M,N} 최소 M번, 최대 N번 발생되는 패턴과 일치시킨다.
$string1 = "Hello World\n";
if ($string1 =~ m/l{1,2}/) {
 print "There exists a substring with at least 1 ";

 print "and at most 2 l's in $string1\n";
}
출력:
There exists a substring with at least 1 and at most 2 l's in Hello World
[...] 가능한 문자열의 집합과 일치시킨다.
$string1 = "Hello World\n";
if ($string1 =~ m/[aeiou]+/) {
  print "$string1 contains one or more vowels.\n";
}
출력:
Hello World
 contains one or more vowels.
| 가능성 있는 항목들을 구별하여 선택한다.
$string1 = "Hello World\n";
if ($string1 =~ m/(Hello|Hi|Pogo)/) {
  print "At least one of Hello, Hi, or Pogo is ";
  print "contained in $string1.\n";
}
출력:
At least one of Hello, Hi, or Pogo is contained in Hello World
.
\b  
$string1 = "Hello World\n";
if ($string1 =~ m/llo\b/) {
  print "There is a word that ends with 'llo'\n";
}
출력:
There is a word that ends with 'llo'
\w "_"를 포함한 영숫자를 일치시킨다.
$string1 = "Hello World\n";
if ($string1 =~ m/\w/) {
  print "There is at least one alphanumeric ";
  print "character in $string1 (A-Z, a-z, 0-9, _)\n";
}
출력:
There is at least one alphanumeric character in Hello World
 (A-Z, a-z, 0-9, _)
\W "_"를 제외하여 영숫자가 아닌 문자열들과 일치시킨다.
$string1 = "Hello World\n";
if ($string1 =~ m/\W/) {
  print "The space between Hello and ";
  print "World is not alphanumeric\n";
}
출력:
The space between Hello and World is not alphanumeric
\s 공백 문자와 일치시킨다.
$string1 = "Hello World\n";
if ($string1 =~ m/\s.*\s/) {
  print "There are TWO whitespace characters, which may";
  print " be separated by other characters, in $string1";
}
출력:
There are TWO whitespace characters, which may be separated by other characters, in Hello World
\S 공백을 제외한 어떠한 것이든 일치시킨다.
$string1 = "Hello World\n";
if ($string1 =~ m/\S.*\S/) {
  print "There are TWO non-whitespace characters, which";
  print " may be separated by other characters, in $string1";
}
출력:
There are TWO non-whitespace characters, which may be separated by other characters, in Hello World
\d 숫자를 일치시킨다.
$string1 = "99 bottles of beer on the wall.";
if ($string1 =~ m/(\d+)/) {
  print "$1 is the first number in '$string1'\n";
}
출력:
99 is the first number in '99 bottles of beer on the wall.'
\D 숫자가 아닌 항목을 일치시킨다.
$string1 = "Hello World\n";
if ($string1 =~ m/\D/) {
  print "There is at least one character in $string1";
  print " that is not a digit.\n";
}
출력:
There is at least one character in Hello World
 that is not a digit.
^ 줄이나 문자열의 시작점과 일치시킨다.
$string1 = "Hello World\n";
if ($string1 =~ m/^He/) {
  print "$string1 starts with the characters 'He'\n";
}
출력:
Hello World
 starts with the characters 'He'
$ 줄이나 문자열의 끝과 일치시킨다.
$string1 = "Hello World\n";
if ($string1 =~ m/rld$/) {
  print "$string1 is a line or string ";
  print "that ends with 'rld'\n";
}
출력:
Hello World
 is a line or string that ends with 'rld'
\A 문자열의 시작점과 일치시킨다. (내부 줄이 아닌)
$string1 = "Hello\nWorld\n";
if ($string1 =~ m/\AH/) {
  print "$string1 is a string ";
  print "that starts with 'H'\n";
}
출력:
Hello
World
 is a string that starts with 'H'
\z 문자열의 끝과 일치시킨다. (내부 줄이 아닌)[12]
$string1 = "Hello\nWorld\n";
if ($string1 =~ m/d\n\z/) {
  print "$string1 is a string ";
  print "that ends with 'd\\n'\n";
}
출력:
Hello
World
 is a string that ends with 'd\n'
[^...] 괄호 안의 항목을 제외한 모든 문자열과 일치시킨다.
$string1 = "Hello World\n";
if ($string1 =~ m/[^abc]/) {
  print "$string1 contains a character other than ";
  print "a, b, and c\n";
}
출력:
Hello World
 contains a character other than a, b, and c
반응형