HackerRank-Python/Regex
Prepare > Regex > Character Class > Matching Specific Characters
stem_sw
2023. 10. 25. 19:30
Matching Specific Characters | HackerRank
Use the [ ] expression to match specific characters.
www.hackerrank.com
문제
Task
You have a test string S.
Your task is to write a regex that will match S with following conditions:
- S must be of length: 6
- First character: 1, 2 or 3
- Second character: 1, 2 or 0
- Third character: x, s or 0
- Fourth character: 3, 0 , A or a
- Fifth character: x, s or u
- Sixth character: . or ,
=> 아래 조건을 만족하는 S를 찾는 정규표현식을 쓰라
코드
Regex_Pattern = r'^[123][120][xs0][30aA][xsu][\.,]$' # Do not delete 'r'.
import re
print(str(bool(re.search(Regex_Pattern, input()))).lower())
노트
문자클래스
대괄호[] 안에 쓰인 것들 중 하라도 매치가 되는지를 판별
- [abc]는 a또는 b또는 c를 의미함
- 하이픈으로 범위를 지정할 수 있음
- [a-c]는 a부터 c까지, 즉 a또는 b또는 c를 의미함
- 대괄호 안에 ^를 써 반대 값을 표현
- [^abc]는 a와 b와 c와 매치가 안되는 것들을 판별
Regex_Pattern = r'^[1-3][0-2][xs0][30aA][xsu][\.,]$' # Do not delete 'r'.
import re
print(str(bool(re.search(Regex_Pattern, input()))).lower())
참조
08-2 정규 표현식 시작하기
정규 표현식에서는 메타 문자(meta characters)를 사용한다. 먼저 메타 문자가 무엇인지 알아보자. [TOC] ## 정규 표현식의 기초, 메타 문자 메타 문자란 원…
wikidocs.net