Prepare > Regex > Character Class > Matching Character Ranges

2023. 10. 27. 18:25HackerRank-Python/Regex

 

Matching Character Ranges | HackerRank

Write a RegEx matching a range of characters.

www.hackerrank.com

 

문제


Task

Write a RegEx that will match a string satisfying the following conditions:

  • The string's length is >= 5.
  • The first character must be a lowercase English alphabetic character.
  • The second character must be a positive digit. Note that we consider zero to be neither positive nor negative.
  • The third character must not be a lowercase English alphabetic character.
  • The fourth character must not be an uppercase English alphabetic character.
  • The fifth character must be an uppercase English alphabetic character.

 

 

 

 

코드


Regex_Pattern = r'^[a-z][1-9][^a-z][^A-Z][A-Z]'	# Do not delete 'r'.

import re

print(str(bool(re.search(Regex_Pattern, input()))).lower())

 

 

 

 

노트


문자클래스([])의 하이픈(-)

문자클래스 안에 쓰인 하이픈은 그 왼쪽과 오른쪽에 쓰인 문자가 각각 하한가, 상한가인 범위를 지정함

  • [a-z]는 소문자 알파벳 전체
  • [0-9]는 0부터 9까지의 정수
  • [a-zA-Z]는 소문자와 대문자 알파벳 전체
  • 부정 문자클래스 ^ 사용 가능
  • [^a-z]는 소문자 알파벳 제외