Variables inside Regexp - ruby

Lets say I have the code:
str = "foobar"
print "Enter in the letters you would like to match: "
match = gets
# Pseudocode:
str =~ /[match]/
I don't want to match the whole string: match, I just want to match each of the letters, like:
str =~ /[aeiou]/
would yield the vowels.
How do I make it so I can match the letters the user inputs?

Try this:
match = gets.chomp # cut off that trailing \n
str =~ /[#{match}]/

Related

matching a double-quote via quote vs pattern

Why does check_char1 fail to find the double-quote?
#!/usr/bin/env ruby
line = 'hello, "bob"'
def check_char1(line, _char)
puts "check_char1 found #{_char} in #{line}" if line =~ /_char/
end
check_char1(line, '"')
def check_char2(line, _char)
puts "check_char2 found #{_char.inspect} in #{line}" if line =~ _char
end
check_char2(line, /"/)
...and can it be made to work using line =~ /_char/? (How should the double-quote be passed to the method?)
If _char is just a string (i.e. no regex pattern matching needed) then just use String#include?
if line.include?(_char)
If you must use a regex for this then Regexp.escape is your friend:
if line =~ /#{Regexp.escape(_char)}/
if line =~ Regexp.new(Regexp.escape(_char))
and if you want _char to be treated like a regex (i.e. '.' matches anything) then drop the Regexp.escape:
if line =~ /#{_char}/
if line =~ Regexp.new(_char)
In check_char1, _char in /_char/ is treated as a literal, not a variable. You need /#{_char}/.
If _char were treated as variable how could one enter a literal in a regex that was the name of a variable, method or constant?

Check if string1 is before string2 on the same line

I am trying to match comment lines in a c#/sql code. CREATE may come before or after /*. They can be on the same line.
line6 = " CREATE /* this is ACTIVE line 6"
line5 = " charlie /* CREATE inside this is comment 5"
In the first case, it will be an active line; in the second, it will be a comment. I probably can do some kind of charindex, but maybe there is a simpler way
regex1 = /\/\*||\-\-/
if (line1 =~ regex1) then puts "Match comment___" + line6 else puts '____' end
if (line1 =~ regex1) then puts "Match comment___" + line5 else puts '____' end
With the regex
r = /
\/ # match forward slash
\* # match asterisk
\s+ # match > 0 whitespace chars
CREATE # match chars
\b # match word break (to avoid matching CREATED)
/ # extended mode for regex def
you can return an array of the comment lines thus:
[line6, line5].select { |l| l =~ r }
#=> [" charlie /* CREATE inside this is comment 5"]

Extract string matching regex

I wanted to extract the value between "#" and ":" as well as after ":" within the following string:
str =
"this is some text
Text#7789347: 4444
some text
text # 7789348 : 666,555
some text
"
Output:
"7789347", " 4444"
"7789348", " 666,555"
I am using the following regex:
(\s)*[t|T][e|E][x|X][t|T](\s)*#(\s)*(\d)*(\s)*:.*
I can select the required field, but I don't know how to get the values.
In case you have to match only floating digits, you can use the /(?mi)^\s*\btext\b.*?#\s*(\d+(?:,\d+)?)\s*:\s*(\d+(?:,\d+)?)$/ regex:
str="""this is some text
Text#7789347: 4444
some text
text # 7789348 : 666,555
some text
"""
puts str.scan(/(?mi)^\s*\btext\b.*?#\s*(\d+(?:,\d+)?)\s*:\s*(\d+(?:,\d+)?)$/)
Output of the demo:
7789347
4444
7789348
666,555
You can scan it like this:
str.each_line{ |line|
a = line.scan(/#(.*):(.*)$/)
puts a[0].inspect if !a.empty?
}
# ["7789347", " 4444"]
# [" 7789348 ", " 666,555"]
To get the values you can use: #\s*(.*?)\s*:\s*(\d+(?:,\d+)*)
if line =~ /#\s*(.*?)\s*:\s*(\d+(?:,\d+)*)/
match1 = $~[1]
match2 = $~[2]
else
match = ""
end
Below Regex may help you:
#\s*(\d+)\s*:\s*([0-9,]*)
DEMO

Extracting word in with regex

I want to replace $word with another word in the following string:
"Hello $word How are you"
I used /\$(.*)/, /\$(.*)(\s)/ , /\$(.* \s)/. Due to *, I get the whole string after $, but I only need that word; I need to escape the space. I tried /s,\b, and few other options, but I cannot figure it out. Any help would be appreciated.
* is a greedy operator meaning it will match as much as it can and still allow the remainder of the regular expression to match. The token .* will greedily match every single character in the string. The regex engine will then advance to the next token \s which matches the last whitespace before the word "you" in the string given you a result of word How are.
You can use \S in place of .* which matches any non-whitespace characters.
\$\S+
Or to simply match only word characters, you can use the following:
\$\w+
If you only want to replace "$world" using a regex, try this:
"Hello $word How are you".gsub(/\$word/, 'other_word')
Or:
"Hello $word How are you".sub('$word',"*")
You can read more for gsub here: http://www.ruby-doc.org/core-2.2.0/String.html#method-i-gsub
Substituting placeholder words for other words is usually not done with a regex but with the % method and a hash:
h = {word: "aaa", other_word: "bbb"}
p "Hello %{word} How are you. %{other_word}. Bye %{word}" % h
# => "Hello aaa How are you. bbb. Bye aaa"
Consider:
>> string = "Hello $word How are you"
=> "Hello $word How are you"
>> replace_regex = /(?<replace_word>\$\w+)/
=> /(?<replace_word>\$\w+)/
>> string.gsub(replace_regex, "Bob")
=> "Hello Bob How are you"
>> string.match(replace_regex)[:replace_word]
=> "$word"
Note:
replace_word is the regex with a named capture group.

How to add string "\n" literally at the end of each line in Ruby?

Here is a string str:
str = "line1
line2
line3"
We would like to add string "\n" to the end of each line:
str = "line1 \n
line2 \n
line3 \n"
A method is defined:
def mod_line(str)
s = ""
str.each_line do |l|
s += l + '\\n'
end
end
The problem is that '\n' is a line feed and was not added to the end of the str even with escape \. What's the right way to add '\n' literally to each line?
String#gsub/String#gsub! plus a very simple regular expression can be used to achieve that:
str = "line1
line2
line3"
str.gsub!(/$/, ' \n')
puts str
Output:
line1 \n
line2 \n
line3 \n
The platform-independent solution:
str.gsub(/\R/) { " \\n#{$~}" }
It will search for line-feeds/carriage-returns and replace them with themselves, prepended by \n.
\n needs to be interpreted as a special character. You need to put it in double quotes.
"\n"
Your attempt:
'\\n'
only escapes the backslash, which is actually redundant. With or without escaping on the backslash, it gives you a backslash followed by the letter n.
Also, your method mod_line returns the result of str.each_line, which is the original string str. You need to return the modified string s:
def mod_line(str)
...
s
end
And by the way, be aware that each line of the original string already has "\n" at the end of each line, so you are adding the second "\n" to each line (making it two lines).
This is the closest I got to it.
def mod_line(str)
s = ""
str.each_line do |l|
s += l
end
p s
end
Using p instead of puts leaves the \n on the end of each line.

Resources