Which python regular expression method should be used to match any character in a-z, 0-9, or !@#$%^&*()?
Which python regular expression method should be used to match any character in a-z, 0-9, or !@#$%^&*()?
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!@#$%^&*()].
A custom character set >>> re.findall( r'[0-9!@#$%^&*()].*?', 'abcdeforghitjrkfgn0129374856ABCDKRF*!&@&#$^()' ) ['0', '1', '2', '9', '3', '7', '4', '8', '5', '6', '*', '!', '&', '@', '&', '#', '$', '^', '(', ')']
Not sure a-z, 0-9 will match on \w but for special characters you need \W
I'll go greedy on this one. It matches any character. So, it covers both w and W
C. A custom character set
I went with Greedy matching, but who knows I'm not a site that is specifically providing answers.
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.