Forum: Ruby Finding un-escaped characters in scan()

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ef53c4abeae8cc46327030bae489a33c?d=identicon&s=25 Fred Phillips (Guest)
on 2007-07-28 15:04
(Received via mailing list)
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?
15a5043475dac9278ae75efb4c71f1f6?d=identicon&s=25 Felix Windt (Guest)
on 2007-07-28 15:21
(Received via mailing list)
> 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?
>

I don't see that behaviour:

irb(main):001:0> "test,,\,test,test2,\,test3".scan(/[^\\],/)
=> ["t,", ",,", "t,", "2,"]
irb(main):002:0>

4 total, as expected.
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2007-07-28 15:22
(Received via mailing list)
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?
>

Works for me:

irb(main):001:0> "test,,\,test".scan(/[^\\],/)
=> ["t,", ",,"]

Todd
Ef53c4abeae8cc46327030bae489a33c?d=identicon&s=25 Fred Phillips (Guest)
on 2007-07-28 15:31
(Received via mailing list)
Hmm, it appears I made a typo in my actual code. But this still isn't
the
result I want.

For instance "test,,test" only returns one comma. And the results you
have
shown above, are returning the wrong number as well.
Cf7cd97cdc8ed7d4ae92965b24f0dfad?d=identicon&s=25 Stefan Rusterholz (apeiros)
on 2007-07-28 15:41
Fred Phillips 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?

Just wanted to point out a mistake the OP and all of the replying people
made:
",,\,," # => ",,,,"
The , is *not* escaped. You need ',,\,,' # => ',,\\,,'

Regards
Stefan
2f4d4f9c35ea851bffb9a9cc2e086365?d=identicon&s=25 Harry Kakueki (Guest)
on 2007-07-28 15:55
(Received via mailing list)
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
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2007-07-28 16:25
(Received via mailing list)
On 7/28/07, Fred Phillips <fophillips1990@gmail.com> wrote:
> Hmm, it appears I made a typo in my actual code. But this still isn't the
> result I want.
>
> For instance "test,,test" only returns one comma. And the results you have
> shown above, are returning the wrong number as well.
>

Fred:

Ah, I see what you are saying in your original post.

"test,,test".scan(/.,/)
=> ["t,"]

It should find ",," too, right?

Interesting behavior and has nothing to do with backslashes, just the
fact that scan won't find it if the character is adjacent to itself.
Like you said, where it starts the next scan, maybe.  I'm not sure how
to get around it without playing around a bit.

Todd
2f4d4f9c35ea851bffb9a9cc2e086365?d=identicon&s=25 Harry Kakueki (Guest)
on 2007-07-28 18:09
(Received via mailing list)
On 7/28/07, Harry Kakueki <list.push@gmail.com> wrote:
> 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
Ef53c4abeae8cc46327030bae489a33c?d=identicon&s=25 Fred Phillips (Guest)
on 2007-07-28 19:01
(Received via mailing list)
Excellent, that works like a charm. Thanks.
A131b672fdbd2a58dce12031ad78b121?d=identicon&s=25 Wolfgang Nádasi-Donner (wonado)
on 2007-07-30 21:50
Fred Phillips 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?

If you can use Ruby 1.9, you simly can express it using 'negative look
behind':


irb(main):001:0> "test,,\\,test".scan(/(?<!\\),/)
=> [",", ","]
irb(main):002:0> "test,,\\,test,test2,\\,test3".scan(/(?<!\\),/)
=> [",", ",", ",", ","]

Wolfgang Nádasi-Donner
This topic is locked and can not be replied to.