0

Need to split a string based on spaces and (). Say for example:

Input String: if condition or (condition-1 or condition-2) then print"

The output should be split into: 'if', 'condition', 'or', '(condition-1 or condition-2)', 'then', 'print'.

When I did the str.split() even the value in the () are getting split based on spaces.

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
venkat
  • 13
  • 1
  • 1
    Welcome to Stack Overflow! Please update your question to show what you have already tried in a [minimal, complete, and verifiable example](https://meta.stackoverflow.com/questions/261592) and add sample input and expected output. For further information, please see [how to ask good questions](https://stackoverflow.com/help/how-to-ask), and take the [tour of the site](https://stackoverflow.com/tour) :) – Gilles Quénot Mar 27 '18 at 12:38
  • 1
    Give me your code. – Piotr Mirosz Mar 27 '18 at 12:38
  • Check this link: https://stackoverflow.com/questions/6868345/python-regex-to-split-on-certain-patterns-with-skip-patterns – Sumit S Chawla Mar 27 '18 at 12:43
  • 1
    str = 'if condition or (condition-1 or condition-2) then print" str.split() gives me if, condition, or , (condition-1, or, condition-2), then, print. I need if, condition, or, (condition-1 or condition-2), then, print. – venkat Mar 27 '18 at 12:48
  • What language do you use? If JavaScript, then read about [`split()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split). – axiac Mar 27 '18 at 12:50
  • python language – venkat Mar 27 '18 at 12:51

2 Answers2

1

Maybe you could check for paranthesis and something in between and then copy that part to another variable and remove it from the original variable. Then just split the first variable and add the two variables together. To do this I would use the re module. python documenting and Automate the boring stuff with python.

This is how I did it:

import re
statement = input(":")
bracketsearcher = re.compile(r'\(\w+\s\w+\)')
bracketplaces = bracketsearcher.findall(statement)
rest = bracketsearcher.sub('',statement)
print (bracketplaces)
rest = rest.split(" ")
print (rest)
Mr. Blue
  • 200
  • 2
  • 2
  • 13
  • Belongs to a comment – Gilles Quénot Mar 27 '18 at 12:41
  • @venkat please check my post again and see if I've solved your problem and if I did please remove your downvote. – Mr. Blue Mar 27 '18 at 13:26
  • Thank you very much Mr.Blue for your response. But for the Input "if condition or (condition-1 or condition-2) then print and (this)" I am getting the below output - ['if', 'condition', 'or', '(condition-1', 'or', 'condition-2)', 'then', 'print', 'and', '(this)']. I need the values between the () intact. The output i need is ['(condition-1 or condition-2)', '(this)', 'if', 'condition', 'or', 'then', 'print', 'and'] – venkat Mar 28 '18 at 06:01
  • Nice answer mr. blue! have a look at mine – Emanuel Graf Mar 28 '18 at 08:35
1

voila

s = "if condition or (condition-1 or condition-2) then print and (this)"

my_list = list()

while (("(") in s):
    my_list.append(s[s.find('(') : (s.find(')')+1)])
    s = (s[ : (s.find('('))]) + (s[s.find(')')+1 : ])

my_list= my_list + (s.split(' '))

print(my_list)
Emanuel Graf
  • 756
  • 17
  • 37