Reputation: 61
How do I break a string into sections in Python? For example, how do I break
IF: /1 =isit= 1\
.show: "1 = 1"
into sections = ['IF:', '/', 1, '=isit=', 1, '\\', '.', 'show:', '"1 = 1"']
PS. This is for basic language development
Tokens (for now):
IF:
(for if) / (to mark the beginning of an expression, or whatever 1==1 is called)=is=
(for =)=isit=
(for ==)\
(for : at the end).
(for a tab)show:
(for print)
strings, ints, bools (true and false), and counting.In other words, things before the : (keywords), after (bools, expressions, objects), and etc.
Upvotes: 1
Views: 190
Reputation: 757
There is no simple solution to this since you can't generalize "split on character X" or "split between characters X and Y".
You will need to write a tokenizer (common synonyms: lexer, parser) that inspects your string character by character (and you will likely need to use state tracking).
Upvotes: 4