Regex and Type | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Regex and Type

input => "hello 1" regex = re.compile("\s*(=>|[-+*\/\%=\(\)]|[A-Za-z_][A-Za-z0-9_]*|[0-9]*\.?[0-9]+)\s*") tokens = regex.findall(expression) print(tokens) output => ["hello", "1"] with this in mind, i was wondering how can i get an uotput where it shows the type of each token, so the desired output would be: [STR: "hello", INT: "1"]

20th Apr 2022, 2:16 PM
Kirill
Kirill - avatar
3 Answers
0
@ravilniki i dont know much about regular expressions, but is there a way to do it with (?P<>) ?
20th Apr 2022, 2:50 PM
Kirill
Kirill - avatar
0
both - hello and 1 - are strings. That's why type() won't work. U could iterate the output and ask for each element isInt()... ...maybe not correct syntax.
20th Apr 2022, 3:03 PM
Oma Falk
Oma Falk - avatar
0
ravilnicki i want to take a string and split it into tokens, so basically how the first output is like, but i would also like to check what each object is, it doesn't have to be STR or INT, it can also be [GROUP1: "hello", GROUP2: "1"] so each token that is a digit goes to group2 and each token that is a full word goes to group1
20th Apr 2022, 3:34 PM
Kirill
Kirill - avatar