1

I'm trying to create string parser that can divide a mathematical equation into the different parts (numbers and operators). Right now, I'm trying to create a list to separate between the different parts.

Ex: ["123", "+", "123"]

However, I'm trying to do it in a way that doesn't require me to write spaces in the input. Instead of inputting: "123 + 123", I want to write: "123+123". How would I split the string between the addition sign and the number and append it to the list?

I'm aware that if I add a space between the "123+123", I would be able to split it, but my goal is to be able to split it without the spaces; just one big block of text.

So far, I've tried splitting it from the addition sign, but that just deletes it which leaves me with ["123", "123"]

equation = input("Equation: ")

>> Equation: 123+123

mylist = []

l.append(equation)

for x in mylist:
    print(x.split("+"))

>> ["123", "123"]
Bottle
  • 25
  • 4
  • 1
    Welcome to SO! How complicated are your expressions? Are subtraction, multiplication and division allowed? Decimals? Leading zeroes? Please enumerate all of the rules you expect to encounter. If the expressions involve nesting, use [this](https://stackoverflow.com/questions/43389684/how-can-i-split-a-string-of-a-mathematical-expressions-in-python), otherwise, `re.findall(r"\d+|\D", s)` should get you started. – ggorlen Jun 22 '19 at 19:30

2 Answers2

1

using re.split you could use a capturing group to keep the delimiter and lookarounds to assert what is on the left and what is on the right is a digit:

(?<=\d)([+])(?=\d)

Explanation

  • (?<=\d) Postitive lookbehind, assert what is directly on the left is a digit
  • ([+]) Capture a + in a group
  • (?=\d) Positive lookahead, assert what is directly to the left is a digit

Regex demo | Python demo

import re

s = "123+123"
s = re.compile("(?<=\d)([+])(?=\d)").split(s)
print(s)

Result

['123', '+', '123']
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

This will do what you want.

import re

input_string = '123+123'

output_string = re.split('(\\+)', input_string)  # additionally can be written r'(\+)'

print(output_string)

Output is ['123', '+', '123']

re.split() behavior is described here in the documentation https://docs.python.org/3/library/re.html#re.split. Basically using parenthesis here makes a capture group and the text pattern is returned as a part of the list.

probat
  • 1,422
  • 3
  • 17
  • 33