From: tunity on
I am using the following regular expression to tokenize a string into
words. It works fine but it also gets rid of the spaces that I would
like to keep. I only need them if they are within single quotes but it
is fine if all spaces are preserved.

Is there a way to do this?


set str {decl a := char 'hello there'; }
regexp -all -inline {\S+} $str
From: Glenn Jackman on
At 2008-06-24 02:36PM, "tunity" wrote:
> I am using the following regular expression to tokenize a string into
> words. It works fine but it also gets rid of the spaces that I would
> like to keep. I only need them if they are within single quotes but it
> is fine if all spaces are preserved.
>
> Is there a way to do this?
>
>
> set str {decl a := char 'hello there'; }
> regexp -all -inline {\S+} $str

I think of CSV format when I hear "split on X except between quotes", so:

package require csv
set str {decl a := char 'hello there'; }
set tokens [csv::split $str { } ']

http://tcllib.sourceforge.net/doc/csv.html

--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous
From: Neil Madden on
tunity wrote:
> I am using the following regular expression to tokenize a string into
> words. It works fine but it also gets rid of the spaces that I would
> like to keep. I only need them if they are within single quotes but it
> is fine if all spaces are preserved.
>
> Is there a way to do this?
>
>
> set str {decl a := char 'hello there'; }
> regexp -all -inline {\S+} $str

set tokens [split $str]

-- Neil
From: Arjen Markus on
On 25 jun, 02:54, Neil Madden <n...(a)cs.nott.ac.uk> wrote:
> tunity wrote:
> > I am using the following regular expression to tokenize a string into
> > words.  It works fine but it also gets rid of the spaces that I would
> > like to keep. I only need them if they are within single quotes but it
> > is fine if all spaces are preserved.
>
> > Is there a way to do this?
>
> > set str {decl a := char 'hello    there'; }
> > regexp -all -inline {\S+} $str
>
> set tokens [split $str]
>
> -- Neil

That won't work: the quotes are "apostrophes" ('), not quotation
marks (").

Regards,

Arjen
From: suchenwi on
On 25 Jun., 09:07, Arjen Markus <arjen.mar...(a)wldelft.nl> wrote:

> > > set str {decl a := char 'hello    there'; }
> > > regexp -all -inline {\S+} $str
>
> > set tokens [split $str]

> That won't work: the quotes are "apostrophes" ('), not quotation
> marks (").

But if the language does not contain quotes, or has them in similar
meaning with apostrophes (like XML attributes), it might indeed make
sense to string map {' \"}