Forum: Ruby text include, searching using a part of the word, storing the whole word if found

Posted by Horváth Alex (faithzz)
on 2013-02-12 17:03
Hey guys,

I'm about the extend my watir testcases with dict error checking for our
tests. I managed to make the check, but I'd like to select the whole
word and store it in a variable or to write in a txt file.

So in practice, I'd like my scripts to
if "???" (dict errors in our system are in the following format
???x.Y???) is found on the current page than select the whole word and
write in a txt file. Also it would be nice to store all of these dict
errors (in case there are several on the page)

Here is what i've got so far

##################### Dict error check
###########################################################
if ie.text.include? "??"
puts "!!!!!!!!!!!"
puts "DICTERROR"
puts "!!!!!!!!!!!"
input = gets
else
end
##################### END OF Dict check
###########################################################

Thanks in advance,
BR,
Alex
Posted by Joel Pearson (virtuoso)
on 2013-02-12 17:11
Gotta love Regexp

irb(main):001:0> e = '???x.Y??? and then ???a.m???'
=> "???x.Y??? and then ???a.m???"
irb(main):002:0> e.scan /\?{3}\w+\.\w+\?{3}/
=> ["???x.Y???", "???a.m???"]

Once you have an array you can do what you like with the contents. There 
are plenty of examples around of writing to text files.
Posted by Horváth Alex (faithzz)
on 2013-02-12 17:15
Joel Pearson wrote in post #1096489:
> Gotta love Regexp
>
> irb(main):001:0> e = '???x.Y??? and then ???a.m???'
> => "???x.Y??? and then ???a.m???"
> irb(main):002:0> e.scan /\?{3}\w+\.\w+\?{3}/
> => ["???x.Y???", "???a.m???"]
>
> Once you have an array you can do what you like with the contents. There
> are plenty of examples around of writing to text files.

Thanks a lot! Yes I'll manage to look after how to write to files, but I 
couldn't find a solution for this by searching.

Br,
Alex
Posted by Joel Pearson (virtuoso)
on 2013-02-12 17:22
Here's a link you can use to play around with the regex and see how it 
works:

http://www.rubular.com/r/ne4tqBVDmu
Posted by "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> (Guest)
on 2013-02-12 17:47
(Received via mailing list)
On Tue, Feb 12, 2013 at 5:03 PM, Horvth Alex <lists@ruby-forum.com> 
wrote:
> errors (in case there are several on the page)
> else
> end

text = "something ???aaa.bbbb??? and something else"
m = text.match(/(\?\?\?[^.]*\.[^?]*)\?\?\?/)
if m
  m.captures.first
end

 => "??aaa.bbbb??"

This is checking that it has a dot in the middle, you might simplify
the regex if you don't need that.

Jesus.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.