• 카테고리

    질문 & 답변
  • 세부 분야

    데이터 분석

  • 해결 여부

    해결됨

re.sub(

21.01.13 10:55 작성 조회수 215

1

 /w를 사용하면 문자랑 숫자를 모두 변환이 가능한건가요?

답변 3

·

답변을 작성해보세요.

1

안녕하세요.

\w는 문자를 의미하는데 정규표현식 문법입니다.  강의 자료에 있는 위키피디아 정규표현식 링크를 보시면 아래 다시 링크로도 보실 수 있는데요.

[정규 표현식 - 위키백과, 우리 모두의 백과사전](https://ko.wikipedia.org/wiki/%EC%A0%95%EA%B7%9C_%ED%91%9C%ED%98%84%EC%8B%9D)

위 링크에서 메타문자 관련 내용만 가져왔어요. 메타문자에서 \w는 문자를 의미하게 됩니다.

정규표현식은 강의에서 언급한 것처럼 책 한 권으로 나올 정도로 배울 내용도 많고 일종의 프로그래밍 언어라 볼 수 있는데요.

외우기 보다는 위키 문서 등을 참고해서 사용하시는 걸 권장해요.

아래 예시는 파이썬은 아니지만 대부분의 프로그래밍 언어에서 정규표현식을 지원하고 심지어 엑셀에서도 지원하기 때문에 아래 문서를 참고해서 활용해 보시면 좋을것 같습니다.

그래서 \w문법을 이해하기 보다는 문서를 참고해서 활용하는 방법으로 접근하는 걸 추천합니다.

기억하기 쉽게 \w를 문자로 이해한다면 word의 약자로 기억해 주세요.

메타문자 설명 예시[11]
. 일반적으로 새 줄을 제외한 모든 어떠한 문자열과도 일치한다.
$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

0

화이팅 입니다! 감사합니다 :)

0

jeonsm9575님의 프로필

jeonsm9575

질문자

2021.01.13

엄청빠른 답변이시네요.감사합니다 열공하겠습니다