Replace with gsub a regexp with accents - ruby-on-rails

I try to get a function for setting < b > around catched strings (case insensitive), like this :
bold_string("Hello everyone","o")
> "Hell<b>o</b> every<b>o</b>ne"
bold_string("HEllo evEryonE", "e")
> "H<b>E</b>llo <b>e</b>v<b>E</b>ryon<b>E<b/>"
Now, my function looks like that :
def bold_string(str, search)
str.gsub(/(#{search})/i, '<b>\1</b>')
end
It works perfectly with the previous examples, but not with the words having some accents. There are the results I expect :
bold_string("Petite bête", "e")
> "P<b>e</b>tit<b>e</b> b<b>ê</b>t<b>e</b>"
bold_string("Petite bête", "ê")
> "P<b>e</b>tit<b>e</b> b<b>ê</b>t<b>e</b>"
In other words, I have to find a regex like /search/i, it says "you have to find the word 'search' or the word 'search' with some accents".
edit :
I see I was too simplist with my example... It should works with string and not simply chars :
bold_string("Petite bête", "êt")
> "P<b>et</b>ite</b> b<b>êt</b>e"
Regards
Pierre
edit2 :
I used the solution of F.J with this new function
def regex_from_string_including_accents(str)
accents = ['aàâ', 'eéèêë', 'oöô' 'iî']
return str.gsub(/./) do |letter|
accent_group = accents.detect{|group| group.include?(letter)}
accent_group ? "[#{accent_group}]" : letter
end
end

You could do something like the following:
def bold_string(str, search)
h = { "e" => "[eéê]", "a" => "[aáâ]" }
regex = search.gsub(/./) {|s| h.fetch(s, s)}
str.gsub(/(#{regex})/i, '<b>\1</b>')
end
Obviously this just shows you how to get started, you will need to fill h with additional accented versions of characters.
Example: http://ideone.com/KukiKc

Related

I know how to reverse a string in ruby without .reverse, but is there a way to only reverse odd indexed characters?

I know how I can reverse a string in ruby without the reverse method
def reverse(string)
string.each_char.inject(""){|str, char| str.insert(0, char) }
end
puts reverse('hello world')
but is there a way I can reverse only the odd indices to look like this.
output: hlloo wlred
that's an interesting problem, here's what I came up with:
def funky_reverse(str)
out = ""
str.length.times{|i| out+= i.even? ? str[i] : str[-i-1]}
out
end
Here's how I'd do it:
def odd_reverse(str)
a = [str, str.reverse]
str.size.times.map {|i| a[i % 2][i] }.join
end
odd_reverse("hello world")
# => "hlloo wlred"
This is pretty simple. For each character index i it alternates (i % 2) taking the next char from either the string or its reverse ([s, s.reverse]).
Working off of Les Nightingill's answer I came up with this which handles both odd and even length strings using the reference_index variable to point to the end of the string or slightly past it as needed.
def funky_reverse(str)
out = ''
reference_index = str.length.odd? ? str.length - 1 : str.length
str.length.times{ |i| out += i.even? ? str[i] : str[reference_index - i] }
out
end
> funky_reverse('hello world')
=> "hlloo wlred"
> funky_reverse('hello world!')
=> "h!lloow rlde"
This looks like a homework question? :D

Simpler way to alternate upper and lower case words in a string

I recently solved this problem, but felt there is a simpler way to do it. I looked into inject, step, and map, but couldn't figure out how to implement them into this code. I want to use fewer lines of code than I am now. I'm new to ruby so if the answer is simple I'd love to add it to my toolbag. Thank you in advance.
goal: accept a sentence string as an arg, and return the sentence with words alternating between uppercase and lowercase
def alternating_case(str)
newstr = []
words = str.split
words.each.with_index do |word, i|
if i.even?
newstr << word.upcase
else
newstr << word.downcase
end
end
newstr.join(" ")
end
You could reduce the number of lines in the each_with_index block by using a ternary conditional (true/false ? value_if_true : value_if_false):
words.each.with_index do |word, i|
newstr << i.even? ? word.upcase : word.downcase
end
As for a different way altogether, you could iterate over the initial string, letter-by-letter, and then change the method when you hit a space:
def alternating_case(str)
#downcase = true
new_str = str.map { |letter| set_case(letter)}
end
def set_case(letter)
#downcase != #downcase if letter == ' '
return #downcase ? letter.downcase : letter.upcase
end
We can achieve this by using ruby's Array#cycle.
Array#cycle returns an Enumerator object which calls block for each element of enum repeatedly n times or forever if none or nil is given.
cycle_enum = [:upcase, :downcase].cycle
#=> #<Enumerator: [:upcase, :downcase]:cycle>
5.times.map { cycle_enum.next }
#=> [:upcase, :downcase, :upcase, :downcase, :upcase]
Now, using the above we can write it as following:
word = "dummyword"
cycle_enum = [:upcase, :downcase].cycle
word.chars.map { |c| c.public_send(cycle_enum.next) }.join("")
#=> "DuMmYwOrD"
Note: If you are new to ruby, you may not be familiar with public_send or Enumberable module. You can use the following references.
Enumberable#cycle
#send & #public_send

Lua gsub regex to replace multiple occurances of characters

I am trying to modify my URL to be clean and friendly by removing more than one occurrence of specific characters
local function fix_url(str)
return str:gsub("[+/=]", {["+"] = "+", ["/"] = "/", ["="] = "="}) --Needs some regex to remove multiple occurances of characters
end
url = "///index.php????page====about&&&lol===you"
output = fix_url(url)
What I would like to achieve the output as is this :
"/index.php?page=about&lol=you"
But instead my output is this :
"///index.php????page====about&&&lol===you"
Is gsub the way i should be doing this ?
I don't see how to do this with one call to gsub. The code below does this by calling gsub once for each character:
url = "///index.php????page====about&&&lol===you"
function fix_url(s,C)
for c in C:gmatch(".") do
s=s:gsub(c.."+",c)
end
return s
end
print(fix_url(url,"+/=&?"))
Here's one possible solution (replace %p with whatever character class you like):
local
function fold(s)
local ans = ''
for s in s:gmatch '.' do
if s ~= ans:sub(-1) then ans = ans .. s end
end
return ans
end
local
function fix_url(s)
return s:gsub('%p+',fold) --remove multiple same characters
end
url = '///index.php????page====about&&&lol===you'
output = fix_url(url)
print(output)

How to separate brackets in ruby?

I've been using the following code for the problem. I'm making a program to change the IUPAC name into structure, so i want to analyse the string entered by the user.In IUPAC name there are brackets as well. I want to extract the compound name as per the brackets. The way I have shown in the end.
I want to modify the way such that the output comes out to be like this and to be stored in an array :
As ["(4'-cyanobiphenyl-4-yl)","5-[(4'-cyanobiphenyl-4-yl)oxy]",
"({5-[(4'-cyanobiphenyl-4-yl)oxy]pentyl}" .... and so on ]
And the code for splitting which i wrote is:
Reg_bracket=/([^(){}\[\]]*)([(){}\[\]])/
attr_reader :obrk, :cbrk
def count_level_br
#xbrk=0
#cbrk=0
if #temp1
#obrk+=1 if #temp1[1]=="(" || #temp1[1]=="[" ||#temp1[1]=="{"
#obrk-=1 if #temp1[1]==")" || #temp1[1]=="]" ||#temp1[1]=="}"
end
puts #obrk.to_s
end
def split_at_bracket(str=nil) #to split the brackets according to Regex
if str a=str
else a=self
end
a=~Reg_bracket
if $& #temp1=[$1,$2,$']
end
#temp1||=[a,"",""]
end
def find_block
#obrk=0 , r=""
#temp1||=["",""]
split_at_bracket
r<<#temp1[0]<<#temp1[1]
count_level_br
while #obrk!=0
split_at_bracket(#temp1[2])
r<<#temp1[0]<<#temp1[1]
count_level_br
puts r.to_s
if #obrk==0
puts "Level 0 has reached"
#puts "Close brackets are #{#cbrk}"
return r
end
end #end
end
end #class end'
I ve used the regex to match the brackets. And then when it finds any bracket it gives the result of before match, after match and second after match and then keeps on doing it until it reaches to the end.
The output which I m getting right now is this.
1
2
1-[(
3
1-[({
4
1-[({5-[
5
1-[({5-[(
4
1-[({5-[(4'-cyanobiphenyl-4-yl)
3
1-[({5-[(4'-cyanobiphenyl-4-yl)oxy]
2
1-[({5-[(4'-cyanobiphenyl-4-yl)oxy]pentyl}
1
1-[({5-[(4'-cyanobiphenyl-4-yl)oxy]pentyl}oxy)
0
1-[({5-[(4'-cyanobiphenyl-4-yl)oxy]pentyl}oxy)carbonyl]
Level 0 has reached
testing ends'
I have written a simple program to match the string using three different regular expressions. The first one will help separate out the parenthesis, the second will separate out the square brackets and the third will give the curly braces. Here is the following code. I hope you will be able to use it in your program effectively.
reg1 = /(\([a-z0-9\'\-\[\]\{\}]+.+\))/ # for parenthesis
reg2 = /(\[[a-z0-9\'\-\(\)\{\}]+.+\])/ # for square brackets
reg3 = /(\{[a-z0-9\'\-\(\)\[\]]+.+\})/ # for curly braces
a = Array.new
s = gets.chomp
x = reg1.match(s)
a << x.to_s
str = x.to_s.chop.reverse.chop.reverse
while x != nil do
x = reg1.match(str)
a << x.to_s
str = x.to_s.chop
end
x = reg2.match(s)
a << x.to_s
str = x.to_s.chop.reverse.chop.reverse
while x != nil do
x = reg2.match(str)
a << x.to_s
str = x.to_s.chop
end
x = reg3.match(s)
a << x.to_s
str = x.to_s.chop.reverse.chop.reverse
while x != nil do
x = reg3.match(str)
a << x.to_s
str = x.to_s.chop
end
puts a
The output is a follows :
ruby reg_yo.rb
4,4'{-1-[({5-[(4'-cyanobiphenyl-4-yl)oxy]pentyl}oxy)carbonyl]-2-[(4'-cyanobiphe‌​nyl-4-yl)oxy]ethylene}dihexanoic acid # input string
({5-[(4'-cyanobiphenyl-4-yl)oxy]pentyl}oxy)carbonyl]-2-[(4'-cyanobiphe‌​nyl-4-yl)
(4'-cyanobiphenyl-4-yl)oxy]pentyl}oxy)
(4'-cyanobiphenyl-4-yl)
[({5-[(4'-cyanobiphenyl-4-yl)oxy]pentyl}oxy)carbonyl]-2-[(4'-cyanobiphe‌​nyl-4-yl)oxy]
[(4'-cyanobiphenyl-4-yl)oxy]pentyl}oxy)carbonyl]
[(4'-cyanobiphenyl-4-yl)oxy]
{-1-[({5-[(4'-cyanobiphenyl-4-yl)oxy]pentyl}oxy)carbonyl]-2-[(4'-cyanobiphe‌​nyl-4-yl)oxy]ethylene}
{5-[(4'-cyanobiphenyl-4-yl)oxy]pentyl}
Update : I have modified the code so as to search for recursive patterns.

One ' to much in string

I try to write to an string something like this:
arr << "Icd3code.create!({:text => '#{variable1}'})" + "\n"
My problem is that variable 1 is an string, that contains an ' :
variable1 = "Ami's house"
So that at the end the ouput of my code is this:
Icd3code.create!({:text => 'Ami's house'})
How you can see now i have one ' to much! I dont know what i can do to avoid this problem! Thanks
If I've understood, you want to loop over some input, building up a list of parameters, which you plan to later use to create some records. If that's the case, I think you're better off using hashes, instead of strings:
# Let's pretend this came from the big, bad, world
inputs = ["Ami's house", "Fred's house", "Jim's house"]
creation_params = []
inputs.each do |input|
creation_params << {:text => input}
end
Then you could create all the Icd3codes, like this:
creation_params.each do |params|
Icd3code.create!(params)
end
Or you could save them in a text file, for later:
File.open('dest', 'w') do |f|
f.write(creation_params.to_json)
end
variable1 = "Ami's house"
puts %Q[Icd3code.create!({:text => "#{variable1}"})] + "\n"
--output:--
Icd3code.create!({:text => "Ami's house"})

Resources