#python 의 #정규 표현 의 그 r 로 패턴을 둘러싸고 있는 녀석‥저녀석‥뭐야‥장난하지마‥답은? (raw string 표기법)

2704 단어 파이썬정규식
주로 백슬래시 대책인 것 같다. 더 특별한 일을 하고 있는지 생각하고 있었어.

파이썬에서 정규식을 사용하는 방법 - Qiita

그런 다음 패턴의 시작 부분에 r을 붙이는 것이 좋습니다. 이해하기 쉽습니다.

re — Regular expression operations — Python 3.7.3 documentation

Regular expressions use the backslash character ('\') to indicate special forms or to allow special characters to be used without invoking their special meaning. This collides with Python's usage of the same character for the same purpose in string literals; match a literal backslash, one might have to write '\\' as the pattern string, because the regular expression must be\, and each backslash must be expressed as\inside a regular Python string literal.

The solution is to use Python's raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'. So r"\n"is a two-character string containing '\' and ' n', while "\n"is a one-character string containing a newline. Usually patterns will be expressed in Python code using this raw string notation.

e.g


>>> re.search('\\\\',  "A\\B\\C")[0]
'\\'
>>> re.search(r'\\',  "A\\B\\C")[0]
'\\'

원시 문자열이란?





Original by Github issue

좋은 웹페이지 즐겨찾기