Regular Expressions and Unicode For Dummies(aka Me) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Regular Expressions and Unicode For Dummies(aka Me)

Hey everyone, so im tackling the spy code coach, and I need to flip a string around (pretty easy), remove symbols (not so easy but I can pull a code line from a past project to do this), and remove numbers. Here is my code I frankensteined out of another project: sentence_nopunct= re.sub(r'[^\w\s]','',sentence, re.UNICODE) As 99% of anyone who reads this likely knows, this string removes non-alphanumeric characters from a string. My question is, can anyone give me a quick explanation on what is going on with this string, and how i might be able to alter it to filter numeric characters as well? (Side note: I forgot to change sentence_nopunct and sentence to something more relevant, so feel free to change those to whatever you see fit)

24th Jun 2020, 9:43 PM
Randy Ziegler
Randy Ziegler - avatar
5 Answers
+ 5
Randy Ziegler Typically, the hard brackets [ ... ] specify any characters to match. However, placing a caret [^ ... ] as the first character within the hard brackets will change this to match anything "except" those characters within the brackets. a-z indicates all lowercase letters between "a" through "z". A-Z will apply to all uppercase letters. \w specifies all alphanumeric characters. \s specifies all whitespace characters like spaces, tabs, and line breaks. \d specifies numeric digits You can mess around with these patterns in the code I just created for you to see how these work. https://code.sololearn.com/c3EroQTAStU7/?ref=app
25th Jun 2020, 3:02 AM
David Carroll
David Carroll - avatar
24th Jun 2020, 9:47 PM
Emanuel Maliaño
Emanuel Maliaño - avatar
+ 2
in addition to playing with code from David Carroll I suggest using https://regex101.com/ to test your expressions
25th Jun 2020, 8:19 AM
SQrL
SQrL - avatar
+ 1
Well, this looks like I'm getting in a bit over my head as a beginner, so I'm just going to try something else lmao
24th Jun 2020, 10:04 PM
Randy Ziegler
Randy Ziegler - avatar
+ 1
Thanks so much, I kind of moved on to other things but maybe later I'll crack down on this
25th Jun 2020, 1:41 PM
Randy Ziegler
Randy Ziegler - avatar