Newbie : Regular expression

Hi,

Can some one please tell me of an example expression which maps to this
regex

“(^#{r}$)”

The ruby statement is

regex = Regexp.new("(^#{r}$)")

r = ‘hello’
str = ‘hello’

if Regexp.new("(^#{r}$)").match str
puts “Yep”
puts $1
end

–output:–
Yep
hello

Try practicing here:

http://rubular.com/

7stud – wrote in post #1081740:

r = ‘hello’
str = ‘hello’

if Regexp.new("(^#{r}$)").match str
puts “Yep”
puts $1
end

–output:–
Yep
hello

Thanks for the prompt reply. Can you explain a bit about #{r}. What that
means ??

greeting = ‘hello’
str = “#{greeting} world”

puts str

–output:–
hello world

It’s called ‘string interpolation’. It’s used to insert the value of a
variable (or an expression) into a string.

On Oct 28, 2012, at 18:14 , Muhammad S. [email protected]
wrote:

Hi,

Can some one please tell me of an example expression which maps to this
regex

“(^#{r}$)”

The ruby statement is

regex = Regexp.new("(^#{r}$)")

it is entirely dependent on what r evaluates to… so no, we can’t
really.

Hi Muhammad,

That depends entirely on what the value of “r” is. The #{} is
interpolation, like in strings.

e.g.

r = /boo/
regexp = Regexp.new("(^#{r}$)")

regexp is now /(^(?-mix:boo)$)/, which is basically the same as saying
/(^(?:boo)$)/, … which is basically the same as /(^boo$)/ in this
context.

In other words, the value of “r” gets included there, with any options
on the regexp “r” set. If “r” is a String, its value is included
verbatim, as if it was part of the regexp source.

Cheers,

Arlen

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