GPYC Exam QuestionsBrowse all questions from this exam

GPYC Exam - Question 16


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

Show Answer
Correct Answer: CD

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

5 comments
Sign in to comment
block933Option: A
Sep 11, 2021

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

kabwitte
Nov 9, 2021

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

JoeSmoOption: C
Nov 4, 2023

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

k4ziOption: C
Aug 1, 2023

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.

digitrealOption: B
Sep 26, 2023

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

a29dc2cOption: C
Feb 7, 2024

C. A custom character set