1. 程式人生 > >python—【爬蟲】學習_2(正則表示式篇)_2(practice)

python—【爬蟲】學習_2(正則表示式篇)_2(practice)

習題來源:hackerrank 

Matching Anything But a Newline(.的用法)

answer :     regex_pattern = r"^(.{3}\.){3}.{3}$"

Matching Digits & Non-Digit Characters(\d和\D的用法)

answer :     Regex_Pattern = r"(\d\d\D){2}\d{4}"    

Matching Whitespace & Non-Whitespace Character
(\s和\S的用法)

answer :       Regex_Pattern = r"\S{2}(\s\S\S){2}"    

Matching Word & Non-Word Character (\w和\W的用法)

answer :       Regex_Pattern = r"\w{3}\W\w{10}\W\w{3}"    

Matching Start & End   (^和$的用法)

answer :       Regex_Pattern = r"^\d\w{4}\.$"

Excluding Specific Characters(^表示除...之外的用法)

Regex_Pattern = r'^\D[^aeiou][^bcDF]\S[^AEOIU][^\.,]$'

Matching Character Ranges ([a-z]range的用法)

Regex_Pattern = r'^[a-z][1-9][^a-z][^A-Z][A-Z]'

Matching {x} Repetitions({x}表示x次)

Regex_Pattern = r'^([a-zA-Z02468]{40}(\s|[13579]){5}$'

Matching {x, y} Repetitions
({x,y}的用法)

Regex_Pattern = r'^\d{1,2}[a-zA-Z]{3,}\.{0,3}$'

w{3,5} : It will match the character w ,  or  times. 
[xyz]{5,} : It will match the character xy or z  or more times. 

Matching Zero Or More Repetitions(*的用法,匹配0or多次)

Regex_Pattern = r'^\d{2,}[a-z]*[A-Z]*$'

Matching One Or More Repetitions (+的用法,匹配1or多次)

Regex_Pattern = r'^\d+[A-Z]+[a-z]+$'

Matching Ending Items($的用法)

Regex_Pattern = r'^[a-zA-Z]*s$'    

Capturing & Non-Capturing Groups(()的用法)

Regex_Pattern = r'(ok){3,}'

Alternative Matching(|的用法)

Regex_Pattern = r'^(Mr\.|Mrs\.|Ms\.|Dr\.|Er\.)[a-zA-Z]+$'

Matching Word Boundaries (\b的用法,匹配單詞的邊界)

Regex_Pattern = r'\b[aeiouAEIOU][a-zA-Z]*\b'

'er\b' 可以匹配"never" 中的 'er',但不能匹配 "verb" 中的 'er'。

Matching Same Text Again & Again(\1\2\3\4的用法)

Regex_Pattern = r'^([a-z]\w\s\W\d\D[A-Z][a-zA-Z][aeiouAEIOU]\S)\1$'

Backreferences To Failed Groups(?的用法,0次or1次)

Regex_Pattern = r"^\d{2}(-?)(\d{2}\1){2}\d{2}$"    

Branch Reset Groups  ((?|regex)的用法)

$Regex_Pattern = '/^\d{2}(?|(\-|\-\-\-)|(\.)|(:))\d{2}\1\d{2}\1\d{2}$/'