2

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?

4
  • @Robert Grant i need if() with stuff inside it. Commented Jun 9, 2015 at 6:05
  • So when you say it should give ['400-', '+34+'] (which is not the IFs with stuff inside) what do you mean? Commented Jun 9, 2015 at 6:06
  • re shoul split it by string like 'IF(3>5,5,5)' Commented Jun 9, 2015 at 6:06
  • So in conclusion, you don't need if() with stuff inside, you need the other stuff? Commented Jun 9, 2015 at 6:07

2 Answers 2

1
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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.