[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Finding un-escaped characters in scan

Harry Kakueki

7/28/2007 1:54:00 PM

On 7/28/07, Fred Phillips <fophillips1990@gmail.com> wrote:
> I have a string that looks like this: "test,,\,test". And I'm trying to
> count the commas which don't follow a \. But when using scan(/[^\\],/) it
> only finds the first comma. I think this is because after scan() has found a
> comma without a backslash, it continues from where it left off, and since
> there's no character before the next comma, it won't match it. Does anyone
> know how to fix it?
>
Are you expecting something like this,

str = "test,,,test"
p str.scan(/,,/) #[",,"]

to give you [",,",",,"] ?

Harry


--
A Look into Japanese Ruby List in English
http://www.ka...

1 Answer

Harry Kakueki

7/28/2007 4:07:00 PM

0

On 7/28/07, Harry Kakueki <list.push@gmail.com> wrote:
> On 7/28/07, Fred Phillips <fophillips1990@gmail.com> wrote:
> > I have a string that looks like this: "test,,\,test". And I'm trying to
> > count the commas which don't follow a \. But when using scan(/[^\\],/) it
> > only finds the first comma. I think this is because after scan() has found a
> > comma without a backslash, it continues from where it left off, and since
> > there's no character before the next comma, it won't match it. Does anyone
> > know how to fix it?
> >
> Are you expecting something like this,
>
> str = "test,,,test"
> p str.scan(/,,/) #[",,"]
>
> to give you [",,",",,"] ?
>
> Harry

If so,
here is one way.
There is probably a more elegant way.

require 'enumerator'
str = "test,,\\,,,\\,,"
str.split(//).each_cons(2){|x|p x[1] if x[0] != "\\" and x[1] == ","}

Harry

--
A Look into Japanese Ruby List in English
http://www.ka...