How to split dot “.†only before equal “=â€

S

Sira PS

I need to split dot only before equal to assign to hash

e.g.

Project.risksPotentialAfterSum=Pot. aft.

result after split should be like this:

{Project=>{risksPotentialAfterSum=>Pot. aft.}}

for now I use str.split(/[.=]/,2) which is has a problem for the value
which comes after equal sign.

any ideas?
 
7

7stud --

str = "Project.risksPotentialAfterSum=Pot. aft."

pieces = str.split(/=/)
puts pieces

--output:--
Project.risksPotentialAfterSum
Pot. aft.
 
S

Sira PS

7stud,

actually i need to split dot before equal sign except after equal sign

the values i need are

Project
risksPotentialAfterSum
Pot. aft.

can i split it the get these result in the same time?
 
R

Roy Zuo

You need regex lookahead syntax

ruby-1.9.2-head > str.split( /\.(?=[^=]*=)/ )
=> ["Project", "risksPotentialAfterSum=Pot. aft."]

Roy
 
7

7stud --

Sira PS wrote in post #994664:
7stud,

actually i need to split dot before equal sign except after equal sign

the values i need are

Project
risksPotentialAfterSum
Pot. aft.

can i split it the get these result in the same time?

str = 'projects.risks.Index.flash_downloading=Downloading.test'

pieces = str.split(/=/)
more_pieces = pieces[0].split(/[.]/)

data = more_pieces << pieces[1]

data.each_slice(2) do |arr|
k, v = arr

if v.nil?
puts "#{k} => nil"
end
end
 
R

Robert Dober

You need regex lookahead syntax

ruby-1.9.2-head > str.split( /\.(?=[^=]*=)/ )
actually the lookahead can be a little simpler here, (?=.*=) does the
trick, or did I miss any particular edge case you had in mind.
Please note that [^=]*= can *always* be expressed as .*??= and often,
that is unless backtracking can occur, as .*?=.

There is an edge case in which neither of our regexen might deliver
the required result, "a.b=c.d=e", but as there is no spec, there is no
solution ;)
Cheers
Robert
 
R

Robert Dober

You need regex lookahead syntax

ruby-1.9.2-head > str.split( /\.(?=[^=]*=)/ )
actually the lookahead can be a little simpler here, (?=.*=) does the
trick, or did I miss any particular edge case you had in mind. Sorry I git confused below
Please note that [^=]*= can *always* be expressed as .*??= and often,
With .*?? I meant (?>.*?)=, which is not really worth of replacing
[^=]*= (but might come in handy if = were a complex subexpression). I
am not so sure about *always* either as there might be further context
which forces the .*? part to be too greedy, maybe.
 
7

7stud --

Sira PS wrote in post #994664:
7stud,

actually i need to split dot before equal sign except after equal sign

the values i need are

Project
risksPotentialAfterSum
Pot. aft.

can i split it the get these result in the same time?

That isn't necessary:


str = 'projects.risks.Index.flash_downloading=Downloading.test'

key_str, val = str.split('=')
keys = key_str.split(/[.]/)

temp = master = {}
last = keys.last

keys.each do |key|
if key == last
temp[key] = val
else
temp = temp[key] = {}
end
end

p master

--output:--
{"projects"=>{"risks"=>{"Index"=>{"flash_downloading"=>"Downloading.test"}}}}
 
7

7stud --

7stud -- wrote in post #994963:
...and you can even get rid of that ugly if check every time through the
loop:


str = 'projects.risks.Index.flash_downloading=Downloading.test'

key_str, val = str.split('=')
keys = key_str.split(/[.]/)

temp = master = {}
last = keys.pop

keys.each do |key|
temp = temp[key] = {}
end

temp[last] = val

p master

--output:--
{"projects"=>{"risks"=>{"Index"=>{"flash_downloading"=>"Downloading.test"}}}}
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top