Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.57 KB | None | 0 0
  1. def escaped_split(s, delim, escape='\\'):
  2.     ret = []
  3.     current = []
  4.     itr = iter(s)
  5.     for ch in itr:
  6.         if ch == escape:
  7.             try:
  8.                 nextCh = next(itr)
  9.                 if nextCh not in delim:
  10.                     current.append(escape)
  11.                 current.append(nextCh)
  12.  
  13.             except StopIteration:
  14.                 current.append(escape)
  15.  
  16.         elif ch in delim:
  17.             ret.append(''.join(current))
  18.             current = []
  19.         else:
  20.             current.append(ch)
  21.     ret.append(''.join(current))
  22.     return ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement