Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. s1 = """ "foo","bar", "foo,bar" """
  2.  
  3. List ["foo","bar","foo,bar"] length 3
  4.  
  5. s2 = """ "foo","bar", 'foo,bar' """
  6.  
  7. pattern = "(('[^']*')|([^,]+))"
  8. re.findall(pattern,s2)
  9. gives [('foo', '', 'foo'), ('bar', '', 'bar'), ("'foo,bar'", "'foo,bar'", '')]
  10.  
  11. Edit
  12. The current pattern support strings like
  13. "foo,bar,foo bar" => [foo,bar,foo bar]
  14. "foo,bar,'foo bar'" => ["foo","bar",'foo bar']
  15. "foo,bar,'foo, bar'" => [foo,bar, 'foo, bar'] #length 3
  16.  
  17. (?:"([^"]+)"|'([^']+)')
  18.  
  19. (?:("[^"]+")|('[^']+')|(w+))
  20.  
  21. >>> import shlex
  22. >>> lex = shlex.shlex(""" "foo","bar", 'foo,bar' """, posix=True)
  23. >>> lex.whitespace = ',' # Only comma will be a splitter
  24. >>> lex.whitespace_split=True # Split by any delimiter defined in whitespace
  25. >>> list(lex) # It is actually an generator
  26. ['foo', 'bar', 'foo,bar']
  27.  
  28. >>> re.findall(r'["|'](.*?)["|']', s1)
  29. ['foo', 'bar', 'foo,bar']
  30. >>> re.findall(r'["|'](.*?)["|']', s2)
  31. ['foo', 'bar', 'foo,bar']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement