Yet another "split string by spaces preserving single quotes" problem

59 views
Skip to first unread message

Massi

unread,
Jul 23, 2012, 12:31:30 PM7/23/12
to re...@googlegroups.com
Hi everyone,
I know this question has been asked thousands of times, but in my case I have some additional requirements to be satisfied. I need to handle substrings in the form 'string with spaces':'another string with spaces' as a single token; I mean, if I have this string:

s ="This is a 'simple test':'string which' shows 'exactly my' problem"

I need to split it as follow (the single quotes must be mantained in
the splitted list):

["This", "is",  "a",  "'simple test':'string which'", "shows", "'exactly my'", "problem"]

I've been succesfully handling this situation (in python) with this regex "(?:'[^']*'|[^' ]+)+", but now I have to change it in order to manage the case in which the sequence \\' implies that the single quote is escaped (i.e. not considered as delimiter). If we consider the example above, the situation is the following:

From: "This is a 'sim\\'ple test':'string which' sh\\'ows 'exactly m\\'y' problem"
To: ["This", "is",  "a",  "'sim\\'ple test':'string which'", "sh\\'ows", "'exactly m\\'y'", "problem"]

Any hints on how to achieve this? Thanks in advance!

Prashant Patole

unread,
Aug 3, 2012, 7:49:15 AM8/3/12
to re...@googlegroups.com

please check if this works
i m into .net so tried it in .net
 
(?:'[^']*(?<=\\\\)'[^']*)+'|[^' ]+
 
 
i coudnt remove the \\ from result. but ithink this what you expect?

 

--
--
Sub, Unsub, Read-on-the-web, tune your personal settings for this Regex forum:
http://groups.google.com/group/regex?hl=en
 
 
 

Prashant Patole

unread,
Aug 3, 2012, 8:21:09 AM8/3/12
to re...@googlegroups.com
oops..
ther was some problem in prev post
 
please use this one.
 
 
([^'\\ ]+(?:\\\\'[^']*)*)|('[^'\\]+)((?:\\\\'[^']*)*)'
Reply all
Reply to author
Forward
0 new messages