jatinkumar patel
jatinkumar patel

Reputation: 2990

python regular expression with re.split()

i have this string equation:

400-IF(3>5,5,5)+34+IF(4>5,5,6)

i want to split it by string 'IF(3>5,5,5)', means 'IF()' syntax, so here i used two if syntax.

so re.split() should give list with length: 2 ['400-', '+34+']

I made re and used as below.

re.split('IF[\(][0-9,a-z,A-Z,\$]*[\>|\<|=|/|%|*|^]?(.*)+[\,][0-9,a-z,A-Z,\$]*[\,][0-9,a-z,A-Z,\$]+[\)]', '400-IF(3>5,5,5)+34+IF(4>5,5,6)
')

But it is not returning proper answer. What is the mistake in my re. I am new in re.

Can anyone modify this re in python?

Upvotes: 2

Views: 304

Answers (2)

Irshad Bhat
Irshad Bhat

Reputation: 8709

>>> z = '400-IF(3>5,5,5)+34+IF(4>5,5,6)'
>>> ' '.join(re.split(r'IF\(.*?\)',z)).split()
['400-', '+34+']

Upvotes: 1

vks
vks

Reputation: 67968

x="400-IF(3>5,5,5)+34+IF(4>5,5,6)"
print [i for i in re.split(r"IF\([^)]*\)",x) if i]

You can simply use this.

Upvotes: 1

Related Questions