Exam GPYC All QuestionsBrowse all questions from this exam
Question 16

Which python regular expression method should be used to match any character in a-z, 0-9, or !@#$%^&*()?

    Correct Answer: C

    To match any character within a specific set of characters in Python regular expressions, a custom character set should be used. This can be accomplished by placing the desired characters within square brackets. For example, to match any character in a-z, 0-9, or !@#$%^&*(), the regular expression would be [a-z0-9!@#$%^&*()].

Discussion
JoeSmoOption: C

A custom character set >>> re.findall( r'[0-9!@#$%^&*()].*?', 'abcdeforghitjrkfgn0129374856ABCDKRF*!&@&#$^()' ) ['0', '1', '2', '9', '3', '7', '4', '8', '5', '6', '*', '!', '&', '@', '&', '#', '$', '^', '(', ')']

block933Option: A

Not sure a-z, 0-9 will match on \w but for special characters you need \W

kabwitte

I'll go greedy on this one. It matches any character. So, it covers both w and W

a29dc2cOption: C

C. A custom character set

digitrealOption: B

I went with Greedy matching, but who knows I'm not a site that is specifically providing answers.

k4ziOption: C

Neither \w or \W would work in this case. A custom set would probably be the way to go. \S would likely work granted spaces were to be excluded.