Using variables in regular expressions

Being lazy, forgetful and generally very bad with regular expressions
leads me to post this question:

How can I use the contents of a variable as a pattern in a regular
expression?

I get a list of files from a set of paths and I want to do the following

repo=list_files paths
repo.collect!{|entry|
entry.gsub(/{variable_with_the root_I_want_to_substitute}/,"")
}

pretty straightforward no?
V.-

···

http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
http://www.freemail.gr - free email service for the Greek-speaking.

Hi –

···

On Mon, 24 Nov 2003, Damphyr wrote:

Being lazy, forgetful and generally very bad with regular expressions
leads me to post this question:

How can I use the contents of a variable as a pattern in a regular
expression?

I get a list of files from a set of paths and I want to do the following

repo=list_files paths
repo.collect!{|entry|
entry.gsub(/{variable_with_the root_I_want_to_substitute}/,“”)
}

Use #{…} (same syntax as string interpolation):

irb(main):002:0> s = “h.”
=> “h.”
irb(main):003:0> /#{s}/.match(“hi”)
=> #MatchData:0x400d2004

David


David A. Black
dblack@wobblini.net

“Damphyr” damphyr@freemail.gr schrieb im Newsbeitrag
news:3FC2104E.7080607@freemail.gr…

Being lazy, forgetful and generally very bad with regular expressions
leads me to post this question:

How can I use the contents of a variable as a pattern in a regular
expression?

I get a list of files from a set of paths and I want to do the following

repo=list_files paths
repo.collect!{|entry|
entry.gsub(/{variable_with_the root_I_want_to_substitute}/,“”)
}

using gsub! is more efficient and better use %r{} because Regexp.quote
does not quote “/”:

repo=list_files paths
repo.each {|entry|
entry.gsub!(%r{#{Regexp.quote(variable_with_the
root_I_want_to_substitute)}},“”)
}

Regards

robert

Don’t need %r{}. A slash “/” in interpolated variables is harmless.
Add anchor, unless removing root from middle of path is desirable.
Add /o modifier:

entry.gsub!(/^#{Regexp.quote(root)}/o, “”)

···

“Robert Klemme” bob.news@gmx.net wrote:

“Damphyr” damphyr@freemail.gr schrieb im Newsbeitrag
news:3FC2104E.7080607@freemail.gr…

repo.collect!{|entry|
entry.gsub(/{variable_with_the root_I_want_to_substitute}/,“”)
}

using gsub! is more efficient and better use %r{} because Regexp.quote
does not quote “/”:

repo.each {|entry|
entry.gsub!(%r{#{Regexp.quote(variable_with_the
root_I_want_to_substitute)}},“”)
}

“Sabby and Tabby” sabbyxtabby@yahoo.com schrieb im Newsbeitrag
news:f5a79bf2.0311251050.1cb4c32d@posting.google.com

“Damphyr” damphyr@freemail.gr schrieb im Newsbeitrag
news:3FC2104E.7080607@freemail.gr…

repo.collect!{|entry|
entry.gsub(/{variable_with_the root_I_want_to_substitute}/,“”)
}

using gsub! is more efficient and better use %r{} because Regexp.quote
does not quote “/”:

repo.each {|entry|
entry.gsub!(%r{#{Regexp.quote(variable_with_the
root_I_want_to_substitute)}},“”)
}

Don’t need %r{}. A slash “/” in interpolated variables is harmless.

True.

Add anchor, unless removing root from middle of path is desirable.

I had that in my first version, but apparently the “^” didn’t make it into
the posting. Thx!

Add /o modifier:

I wouldn’t do that if the rx was in a method that received the path as
parameter. Could lead to surprising effects. :slight_smile:

entry.gsub!(/^#{Regexp.quote(root)}/o, “”)

Cheers

robert
···

“Robert Klemme” bob.news@gmx.net wrote: