简体   繁体   中英

Split a string in python based on a set of characters

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]

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']

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']

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM