Python regex — extraneous matchings

后端 未结 5 1265
执念已碎
执念已碎 2021-01-18 07:17

I want to split a string using -, +=, ==, =, +, and white-space as delimiters. I want to keep the delimiter

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-18 08:21

    Try this:

    def tokenize(s):
      import re
      pattern = re.compile("(\-|\+\=|\=\=|\=|\+)|\s+")
      x = pattern.split(s)
      result = []
      for item in x:
        if item != '' and item != None:
          result.append(item)
      return result
    
    print(tokenize("hello-+==== =+  there"))
    

提交回复
热议问题