# How to split dot “.” only before equal “=”

**URL:** <https://rubytalk.org/t/how-to-split-dot-only-before-equal/62515>\
**Category:** ruby-talk\
**Created:** [23 April 2011 17:05 UTC](https://rubytalk.org/t/how-to-split-dot-only-before-equal/62515 "2011-04-23T17:05:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sira\_PS](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Sira\_PS](https://rubytalk.org/u/Sira_PS)\
**Post date:** [23 April 2011 17:05 UTC](https://rubytalk.org/t/how-to-split-dot-only-before-equal/62515/1 "2011-04-23T17:05:22Z")

</div>

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?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [23 April 2011 17:08 UTC](https://rubytalk.org/t/how-to-split-dot-only-before-equal/62515/2 "2011-04-23T17:08:48Z")

</div>

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

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

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Sira\_PS](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Sira\_PS](https://rubytalk.org/u/Sira_PS)\
**Post date:** [23 April 2011 17:31 UTC](https://rubytalk.org/t/how-to-split-dot-only-before-equal/62515/3 "2011-04-23T17:31:22Z")

</div>

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?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Sira\_PS](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Sira\_PS](https://rubytalk.org/u/Sira_PS)\
**Post date:** [24 April 2011 08:02 UTC](https://rubytalk.org/t/how-to-split-dot-only-before-equal/62515/4 "2011-04-24T08:02:18Z")

</div>

I already got the solution. thx all

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Roy\_Zuo](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@Roy\_Zuo](https://rubytalk.org/u/Roy_Zuo)\
**Post date:** [23 April 2011 17:38 UTC](https://rubytalk.org/t/how-to-split-dot-only-before-equal/62515/5 "2011-04-23T17:38:03Z")

</div>

You need regex lookahead syntax

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

Roy

> **···**
>
> On Sun, Apr 24, 2011 at 02:31:22AM +0900, Sira PS wrote:
> 
> > 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?
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> / It now costs more to amuse a child than it once did to educate his \  
> \ father. /  
> --------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\ ,\_\_,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\ (oo)\_\_\_\_  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(\_\_) )\  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\>\>--|| \*

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [23 April 2011 22:57 UTC](https://rubytalk.org/t/how-to-split-dot-only-before-equal/62515/6 "2011-04-23T22:57:06Z")

</div>

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|  
&nbsp;&nbsp;k, v = arr

&nbsp;&nbsp;if v.nil?  
&nbsp;&nbsp;&nbsp;&nbsp;puts "#{k} =\> nil"  
&nbsp;&nbsp;end  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [25 April 2011 21:27 UTC](https://rubytalk.org/t/how-to-split-dot-only-before-equal/62515/7 "2011-04-25T21:27:19Z")

</div>

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|  
&nbsp;&nbsp;if key == last  
&nbsp;&nbsp;&nbsp;&nbsp;temp[key] = val  
&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;temp = temp[key] = {}  
&nbsp;&nbsp;end  
end

p master

--output:--  
{"projects"=\>{"risks"=\>{"Index"=\>{"flash\_downloading"=\>"Downloading.test"}}}}

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [24 April 2011 07:33 UTC](https://rubytalk.org/t/how-to-split-dot-only-before-equal/62515/8 "2011-04-24T07:33:15Z")

</div>

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

> **···**
>
> On Sat, Apr 23, 2011 at 7:38 PM, Roy Zuo \<roylzuo@gmail.com\> wrote:
> 
> > You need regex lookahead syntax
> > 
> > ruby-1.9.2-head \> str.split( /\.(?=[^=]\*=)/ )

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [26 April 2011 00:46 UTC](https://rubytalk.org/t/how-to-split-dot-only-before-equal/62515/9 "2011-04-26T00:46:58Z")

</div>

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|  
&nbsp;&nbsp;temp = temp[key] = {}  
end

temp[last] = val

p master

--output:--  
{"projects"=\>{"risks"=\>{"Index"=\>{"flash\_downloading"=\>"Downloading.test"}}}}

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [24 April 2011 08:09 UTC](https://rubytalk.org/t/how-to-split-dot-only-before-equal/62515/10 "2011-04-24T08:09:40Z")

</div>

> > 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.

> **···**
>
> On Sun, Apr 24, 2011 at 9:33 AM, Robert Dober \<robert.dober@gmail.com\> wrote:
> 
> > On Sat, Apr 23, 2011 at 7:38 PM, Roy Zuo \<roylzuo@gmail.com\> wrote:
> 
> --  
> The 1,000,000th fibonacci number contains '42' 2039 times; that is  
> almost 30 occurrences more than expected (208988 digits).  
> N.B. The 42nd fibonacci number does not contain '1000000' that is  
> almost the expected 3.0e-06 times.
