[yanq = yet another newbie question] correct syntax fovariable and regexp

Y

Yvon Thoraval

i have a regex like :

rex = Regexp.new("^(.*)\s+Bourgogne[\\s*|\\s+(.*)]")

but i want to change the string "Bourgogne" to the third arg of the
script (needle = ARGV[2].to_s)

what is the correct syntax in order to replace "Bourgogne" by that
variable ?

Also, i do have a test :

if line =~ /^(.*)\s+Bourgogne\s*(.*)/

using rex is the correct syntax :

if line =~ rex

???
 
M

Markus

I am not sure exactly what you are asking, so I will offer multiple
suggestions and you can see what works best for you:

line.gsub!(/Bourgogne/,needle)

changes all occurrences

line.sub!(/Bourgogne/,needle)

changes the first occurrence (if any), as does

line[/Bourgogne/] = needle

or if you want to keep the original string you can write (note, no "!"):

new_line = line.sub(/Bourgogne/,needle)

or maybe you would rather:

rex = Regexp.new("^(.*)Bourgogne(.*)")
if line =~ rex
line = $1 + needle + $2
end

Hope this helps.

-- Markus
 
Y

Yvon Thoraval

Markus said:
I am not sure exactly what you are asking, so I will offer multiple
suggestions and you can see what works best for you:
Thanks for your answer, i need to clarify...

I want, from a given file, output to a new file the lines containing a
word (the example was "Bourgogne") given as the third arg of the script.

Notice i'm able to do that in a "static" way that's to say changing the
word in the script's regexp, what i don't know is the correct syntax to
replace this word by a variable in the regexp, the variable being a
String (ARGV[2].to_s).
 
R

Robert Klemme

Yvon Thoraval said:
Markus said:
I am not sure exactly what you are asking, so I will offer multiple
suggestions and you can see what works best for you:
Thanks for your answer, i need to clarify...

I want, from a given file, output to a new file the lines containing a
word (the example was "Bourgogne") given as the third arg of the script.

Notice i'm able to do that in a "static" way that's to say changing the
word in the script's regexp, what i don't know is the correct syntax to
replace this word by a variable in the regexp, the variable being a
String (ARGV[2].to_s).

needle = ARGV[2]
rx = %r{^(.*)\s+#{needle}[\\s*|\\s+(.*)]}

If you want to be sure that meta symbols in needle are not interpreted you
can do this:

rx = %r{^(.*)\s+#{Regexp.escape needle}[\\s*|\\s+(.*)]}

What strikes me odd is the rest of your regexp, especially the square
brackets. If you just want to match a certain word as word in the line
you should probably use these ones:

rx = %r{\b#{needle}\b}
rx = %r{\b#{Regexp.escape needle}\b}

"\b" matches a word boundary.

Kind regards

robert
 
Y

Yvon Thoraval

Robert Klemme said:
needle = ARGV[2]
rx = %r{^(.*)\s+#{needle}[\\s*|\\s+(.*)]}

If you want to be sure that meta symbols in needle are not interpreted you
can do this:

rx = %r{^(.*)\s+#{Regexp.escape needle}[\\s*|\\s+(.*)]}

What strikes me odd is the rest of your regexp, especially the square
brackets.
If you just want to match a certain word as word in the line
you should probably use these ones:

rx = %r{\b#{needle}\b}
rx = %r{\b#{Regexp.escape needle}\b}

"\b" matches a word boundary.

thanks for all, crystall clear to me °;)
(i had forgotten the magic of "\b")

and works great !!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top