Regex noob question

Using a regular expression, how would I match “howdy” in the
following?

“begin howdy end begin hello end” =~ /begin (.*) end/

match = $1

current value of match = “howdy end begin hello”

A handy regex tool: http://www.rubular.com

On Jul 14, 2008, at 9:29 PM, gsterndale wrote:

Using a regular expression, how would I match “howdy” in the
following?

“begin howdy end begin hello end” =~ /begin (.*) end/

match = $1

current value of match = “howdy end begin hello”

Assuming it’s that exact string…

“begin howdy end begin hello end” =~ /begin (.*?) /; $1
=> “howdy”

But I suspect that won’t work for your real usage…

You’re right. Unfortunately, that won’t work for me. However, the
following will!

“begin hi there end begin hello end” =~ /begin (.*?) end/; $1
=> “hi there”

Thanks Philip

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs