Prepare > Regex > Introduction > Matching Anything But a Newline
2023. 10. 6. 08:00ㆍHackerRank-Python/Regex
Matching Anything But a Newline | HackerRank
Use [.] in the regex expression to match anything but a newline character.
www.hackerrank.com
문제
Task
You have a test string S.
Your task is to write a regular expression that matches only and exactly strings of form: abc.def.hki.xkj, where each variable a, b, c, d, e, f, h, k, i, j, k, x can be any single character except the newline.
=> 문자 3자리 와 .(점) 이 4번 반복되는 구조를 테스트 문자열 S에서 찾아라
코드
regex_pattern = r"^.{3}\..{3}\..{3}\..{3}$" # Do not delete 'r'.
import re
import sys
test_string = input()
match = re.match(regex_pattern, test_string) is not None
print(str(match).lower())
노트
regex_pattern = r".{3}\..{3}\..{3}\..{3}" # Do not delete 'r'.
- 시작과 끝을 정해주지 않으면 123.123.123.123.123.123 과 같은 더 긴 문자열도 참으로 매치 해버림