Reputation: 45
I need to split a string based on some set of characters using python. For example
String = "A==B AND B==C OR C!=A OR JP Bank==Chase"
I don't want to split the string based on space, since JP and Chase will form two different words. So, I need to split based on ==,!=,AND,OR. Expected output
[A,==,B,AND,B,==,C,OR,C,!=,A,OR,JP Bank,==,Chase]
Upvotes: 0
Views: 342
Reputation: 61225
Using re.split
with a capture group in your regular expression.
import re
s = "A==B AND B==C OR C!=A OR JP Bank==Chase"
pat = re.compile(r'(==|!=|AND|OR)')
pat.split(s)
Result
['A', '==', 'B ', 'AND', ' B', '==', 'C ', 'OR', ' C', '!=', 'A ', 'OR', ' JP Bank', '==', 'Chase']
Upvotes: 3
Reputation: 1727
like this?
import re
inStrint = "A==B AND B==C OR C!=A OR JP Bank==Chase"
outList = re.split( '(==|!=|OR|AND)', inString)
outList = map( lambda x: x.strip(), outList)
Upvotes: 0
Reputation: 174706
You could try re.split
function. \s*
before and after (AND|OR|[!=]=)
helps to remove the spaces also.
>>> s = "A==B AND B==C OR C!=A OR JP Bank==Chase"
>>> re.split('\s*(AND|OR|[!=]=)\s*', s)
['A', '==', 'B', 'AND', 'B', '==', 'C', 'OR', 'C', '!=', 'A', 'OR', 'JP Bank', '==', 'Chase']
Upvotes: 2