Using regular expressions with variables

6 views
Skip to first unread message

simon

unread,
Jun 10, 2008, 11:58:57 AM6/10/08
to Watir General
Hello,

I'm using Watir to attach to an IE window using a regular expression,

ie = Watir::IE.attach(:title, /Google/)

Which works perfectly fine.

The problem is the string used (google here) has to be stored in a
variable.

If i try something like

myvar = "/Google/"
ie = Watir::IE.attach(:title, myvar)

Watir of course attaches to a window with the title /Google/.
Does anyone has an idea about the correct syntax to use?


Thanks
Simon

Paul Rogers

unread,
Jun 10, 2008, 12:06:43 PM6/10/08
to watir-...@googlegroups.com
you're close:


myvar = /G/


ie = Watir::IE.attach(:title, myvar)

will attach to a windo that has a G anywhere in its title

Paul

Mark Anderson

unread,
Jun 10, 2008, 12:13:57 PM6/10/08
to watir-...@googlegroups.com

You want to store the regular expression in the variable, so I tried the
following and it works:

Alex Collins

unread,
Jun 10, 2008, 6:01:48 PM6/10/08
to watir-...@googlegroups.com
Simon,

It may also be helpful to look at the Ruby Documentation (RDoc) for the
Regexp (Regular Expression) class.
http://www.ruby-doc.org/core/classes/Regexp.html

In addition to creating a regular expression using the /pattern/
notation, a few quick helpers from an interactive ruby (irb) session:

# a is a string
irb(main):001:0> a = "foofar"
=> "foofar"

# create a new regular expression, b, from the string in a
# this is the same as writing b = Regexp.new "foofar"
irb(main):002:0> b = Regexp.new a
=> /foofar/

# obtain the string between the / characters
irb(main):003:0> b.source
=> "foofar"

# the regular expression itself
irb(main):004:0> b
=> /foofar/

# a string representation of the regexp as you would expect to see it in
code
irb(main):005:0> b.inspect
=> "/foofar/"

# a different string representation of the regexp
irb(main):006:0> b.to_s
=> "(?-mix:foofar)"

Hope this helps,

Alex

simon

unread,
Jun 11, 2008, 5:58:45 AM6/11/08
to Watir General
Thanks everyone,

It works fine. Actually my variable is of string type, coming form a
third party tool,
so I hade to use Alex's hint :


myvar = "Google"
myvar_regexp = Regexp.new myvar
ie = Watir::IE.attach(:title, myvar_regexp)

And that's it!
thanks again for your help
Simon




On Jun 11, 12:01 am, Alex Collins <a.j.collins...@gmail.com> wrote:
> Simon,
>
> It may also be helpful to look at the Ruby Documentation (RDoc) for the
> Regexp (Regular Expression) class.http://www.ruby-doc.org/core/classes/Regexp.html
> > On Tue, Jun 10, 2008 at 9:58 AM, simon <simonpiccio...@yahoo.fr> wrote:
>
> >> Hello,
>
> >> I'm using Watir to attach to an  IE window using a regular expression,
>
> >>  ie = Watir::IE.attach(:title, /Google/)
>
> >> Which works perfectly fine.
>
> >> The problem is the string used (google here) has to be stored in a
> >> variable.
>
> >> If i try something like
>
> >> myvar = "/Google/"
> >> ie = Watir::IE.attach(:title, myvar)
>
> >> Watir of course attaches to a window with the title /Google/.
> >> Does anyone has an idea about the correct syntax to use?
>
> >> Thanks
> >> Simon- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages