[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Return number of matches

Chris Causer

2/11/2008 2:24:00 PM

This seems like an easy question but I cannot find any documentation on
it anywhere on the web.

I want to write a script that will count the number of dollars in a
string, excluding escaped dollars (ie. "\$".) My first hack was to use
split and count the number of elements in the array, but I came unstuck
trying to exclude \$. Surely there's a nice simple elegant solution?

I'm very (as in this morning) new to Ruby so apologies if this is a
trivial question and I'm posting on the wrong forum.

Chris
--
Posted via http://www.ruby-....

9 Answers

Jano Svitok

2/11/2008 2:42:00 PM

0

On Feb 11, 2008 3:24 PM, Christopher Causer <chy.causer@gmail.com> wrote:
> This seems like an easy question but I cannot find any documentation on
> it anywhere on the web.
>
> I want to write a script that will count the number of dollars in a
> string, excluding escaped dollars (ie. "\$".) My first hack was to use
> split and count the number of elements in the array, but I came unstuck
> trying to exclude \$. Surely there's a nice simple elegant solution?

Easy solution: count $ and subtract \$ ;-)

string.scan(/\$/).size - string.scan(/\\\$/).size

It's possible to do it in one regex, but it won't be nice and elegant
and I don't have the time now to try it...

Chris Causer

2/11/2008 2:48:00 PM

0

Jano Svitok wrote:

> It's possible to do it in one regex, but it won't be nice and elegant
> and I don't have the time now to try it...

I was so psyched in doing it in one regexp that I completely missed the
absolute beauty that you posted. Thanks a lot for the help.
--
Posted via http://www.ruby-....

Robert Klemme

2/11/2008 5:04:00 PM

0

On 11.02.2008 15:48, Christopher Causer wrote:
> Jano Svitok wrote:
>
>> It's possible to do it in one regex, but it won't be nice and elegant
>> and I don't have the time now to try it...
>
> I was so psyched in doing it in one regexp that I completely missed the
> absolute beauty that you posted. Thanks a lot for the help.

With 1.9 you can use negative lookbehind. If that's not available then
it becomes ugly (if it is possible at all). The simplest pre 1.9 single
pass solution that comes to mind is this

irb(main):012:0> c=0
=> 0
irb(main):013:0> "$\\$$".scan(/\$/) { c+=1 unless $`[-1] == ?\\ }
=> "$\\$$"
irb(main):014:0> c
=> 2

Kind regards

robert

William James

2/11/2008 9:40:00 PM

0

On Feb 11, 8:41 am, Jano Svitok <jan.svi...@gmail.com> wrote:
> On Feb 11, 2008 3:24 PM, Christopher Causer <chy.cau...@gmail.com> wrote:
>
> > This seems like an easy question but I cannot find any documentation on
> > it anywhere on the web.
>
> > I want to write a script that will count the number of dollars in a
> > string, excluding escaped dollars (ie. "\$".) My first hack was to use
> > split and count the number of elements in the array, but I came unstuck
> > trying to exclude \$. Surely there's a nice simple elegant solution?
>
> Easy solution: count $ and subtract \$ ;-)
>
> string.scan(/\$/).size - string.scan(/\\\$/).size
>
> It's possible to do it in one regex, but it won't be nice and elegant
> and I don't have the time now to try it...

That fails when the backslash preceding a $
is itself escaped.

>> irb --prompt xmp
string = "$\\\\$"
==>"$\\\\$"
string.scan(/\$/).size - string.scan(/\\\$/).size
==>1

Try this:

p DATA.read.scan(/(\\.|[$])/).flatten.grep(/^.$/).size

__END__
$\\$\$$

Chris Causer

2/12/2008 9:11:00 AM

0

William James wrote:
> On Feb 11, 8:41 am, Jano Svitok <jan.svi...@gmail.com> wrote:
>> Easy solution: count $ and subtract \$ ;-)
>>
>> string.scan(/\$/).size - string.scan(/\\\$/).size
>>
>> It's possible to do it in one regex, but it won't be nice and elegant
>> and I don't have the time now to try it...
>
> That fails when the backslash preceding a $
> is itself escaped.
>
>>> irb --prompt xmp
> string = "$\\\\$"
> ==>"$\\\\$"
> string.scan(/\$/).size - string.scan(/\\\$/).size
> ==>1

Erm, not sure what you're trying to say. One is the answer I would have
wanted. Thanks anyway for the one liner though.

Just out of interest, why doesn't my first effort work?

string.scan(/[^\\]\$/).size

?
--
Posted via http://www.ruby-....

ThoML

2/12/2008 9:47:00 AM

0

> string.scan(/[^\\]\$/).size

This fails on input like "$$$$", since the string will be split into "$
$", "$$". It will also not find the first $ in the string but this
could be amended by using (^|[^\\]).

Jano Svitok

2/12/2008 10:09:00 AM

0

On Feb 12, 2008 10:10 AM, Christopher Causer <chy.causer@gmail.com> wrote:
> William James wrote:
> > On Feb 11, 8:41 am, Jano Svitok <jan.svi...@gmail.com> wrote:
> >> Easy solution: count $ and subtract \$ ;-)
> >>
> >> string.scan(/\$/).size - string.scan(/\\\$/).size
> >>
> >> It's possible to do it in one regex, but it won't be nice and elegant
> >> and I don't have the time now to try it...
> >
> > That fails when the backslash preceding a $
> > is itself escaped.
> >
> >>> irb --prompt xmp
> > string = "$\\\\$"
> > ==>"$\\\\$"
> > string.scan(/\$/).size - string.scan(/\\\$/).size
> > ==>1
>
> Erm, not sure what you're trying to say. One is the answer I would have
> wanted. Thanks anyway for the one liner though.

William's tring to say that you have three interesting items in your
string, and you haven't counted one:
a plain dollar $, escaped dollar \$ and escaped backslash \\. If
escaped backslash precedes unescaped dollar, \\$
in your definition of the problem (and in my solution) it is not counted.

William James

2/12/2008 3:14:00 PM

0

On Feb 12, 3:10 am, Christopher Causer <chy.cau...@gmail.com> wrote:
> William James wrote:
> > On Feb 11, 8:41 am, Jano Svitok <jan.svi...@gmail.com> wrote:
> >> Easy solution: count $ and subtract \$ ;-)
>
> >> string.scan(/\$/).size - string.scan(/\\\$/).size
>
> >> It's possible to do it in one regex, but it won't be nice and elegant
> >> and I don't have the time now to try it...
>
> > That fails when the backslash preceding a $
> > is itself escaped.
>
> >>> irb --prompt xmp
> > string = "$\\\\$"
> > ==>"$\\\\$"
> > string.scan(/\$/).size - string.scan(/\\\$/).size
> > ==>1
>
> Erm, not sure what you're trying to say. One is the answer I would have
> wanted. Thanks anyway for the one liner though.

You said that you wanted to count all dollar signs except the
escaped ones. So the correct answer is 2. The first backslash
escapes the second one, which escapes nothing.

Chris Causer

2/12/2008 5:49:00 PM

0

William James wrote:

> You said that you wanted to count all dollar signs except the
> escaped ones. So the correct answer is 2. The first backslash
> escapes the second one, which escapes nothing.

Yes, thanks, I realize now. 1 was the answer I would have wanted because
it wasn't preceded by a space, but I could have made that much clearer
in my question.

Thanks a lot guys. You've been most helpful. Have loads more basic
questions like that. Is this the right forum for such questions?
--
Posted via http://www.ruby-....