Prepare > Regex > Character Class > Excluding Specific Characters

2023. 10. 26. 20:10HackerRank-Python/Regex

 

Excluding Specific Characters | HackerRank

Use the [^] character class to exclude specific characters.

www.hackerrank.com

 

문제


Task

You have a test string S.
Your task is to write a regex that will match  with the following conditions:

  • S must be of length 6.
  • First character should not be a digit (1,2,3,4,5,6,7,8,9 or 0).
  • Second character should not be a lowercase vowel (a,i,e,u or o).
  • Third character should not be bcD or F.
  • Fourth character should not be a whitespace character ( \r, \n, \t, \f or <space> ).
  • Fifth character should not be a uppercase vowel (A,E,U,I or U).
  • Sixth character should not be a . or , symbol.

 

 

 

 

코드


Regex_Pattern = r'^[^0-9][^auioe][^bcDF][^\s][^AEIOU][^\.,]$'	# Do not delete 'r'.

import re

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

 

 

 

 

노트


^ 부정 문자 클래스

The negated character class [^] matches any character that is not in the square brackets.

 

 

 

Regex_Pattern = r'^\D[^auioe][^bcDF]\S[^AEIOU][^\.,]$'	# Do not delete 'r'.

import re

print(str(bool(re.search(Regex_Pattern, input()))).lower())
  • [^0-9] 를 \D로
  • [^\s]를 \S로
  • S는 무조건 6글자 이므로 ^와 $로 길이까지 지정