Y
Yusuf Celik
Hi,
I wonder if there is a way of counting a word in a line using ruby
regexp.
Such as how many "Hi"'s in a line.
Let's say when I enter a line
"Hello and Hi there hey hear my Hi"
It should give me 2, because there are 2 "Hi"'s.
I have something like this but looks awkward.
while line=gets.chomp
cnt = 0
line.split.each {
|v|
if v == 'Hi' then
cnt +=1
end
}
break if cnt > 2
end
Thanks in advance
Yusuf Celik
I wonder if there is a way of counting a word in a line using ruby
regexp.
Such as how many "Hi"'s in a line.
Let's say when I enter a line
"Hello and Hi there hey hear my Hi"
It should give me 2, because there are 2 "Hi"'s.
I have something like this but looks awkward.
while line=gets.chomp
cnt = 0
line.split.each {
|v|
if v == 'Hi' then
cnt +=1
end
}
break if cnt > 2
end
Thanks in advance
Yusuf Celik