How to separate parentheses in ruby?
I used the following code for the problem. I am making a program to change the IUPAC name to a structure, so I want to parse the string entered by the user. There are also parentheses in the IUPAC name. I want to extract the distinguished name in parentheses. The way I showed at the end.
I want to change the way the output is output this way and store it in an array:
As ["(4'-cyanobiphenyl-4-yl)", "5 - [(4'-cyanobiphenyl-4-yl) oxy]", "({5 - [(4'-cyanobiphenyl-4-yl) oxy] pentyl} "etc.]
And the splitting code I wrote:
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 have used regex to match parentheses. And then when he finds any parenthesis, he gives the result before the match, after the match and the second after the match, and then continues to do so until it comes to the end.
The output 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'
source to share
I wrote a simple program to match a string using three different regular expressions. The first one will help to highlight the parentheses, the second will separate the square brackets, and the third will give the curly braces. Here's the following code. Hope you can use it effectively in your program.
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 as follows:
ruby reg_yo.rb
4,4'{-1-[({5-[(4'-cyanobiphenyl-4-yl)oxy]pentyl}oxy)carbonyl]-2-[(4'-cyanobiphenyl-4-yl)oxy]ethylene}dihexanoic acid # input string
({5-[(4'-cyanobiphenyl-4-yl)oxy]pentyl}oxy)carbonyl]-2-[(4'-cyanobiphenyl-4-yl)
(4'-cyanobiphenyl-4-yl)oxy]pentyl}oxy)
(4'-cyanobiphenyl-4-yl)
[({5-[(4'-cyanobiphenyl-4-yl)oxy]pentyl}oxy)carbonyl]-2-[(4'-cyanobiphenyl-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'-cyanobiphenyl-4-yl)oxy]ethylene}
{5-[(4'-cyanobiphenyl-4-yl)oxy]pentyl}
Update: I changed the code to find recursive patterns.
source to share