hprogram
hprogram

Reputation: 61

how do i break a string into sections python

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):

  1. IF: (for if) / (to mark the beginning of an expression, or whatever 1==1 is called)
  2. =is= (for =)
  3. =isit= (for ==)
  4. \ (for : at the end)
  5. . (for a tab)
  6. 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

Answers (1)

Julian
Julian

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

Related Questions