# Regular expression reformating a value

**URL:** <https://www.ruby-forum.com/t/regular-expression-reformating-a-value/70357>\
**Category:** Ruby\
**Created:** [September 28, 2006, 5:52pm UTC](https://www.ruby-forum.com/t/regular-expression-reformating-a-value/70357 "2006-09-28T17:52:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevenreplica](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c0e974/32.png) [@stevenreplica](https://www.ruby-forum.com/u/stevenreplica)\
**Post date:** [September 28, 2006, 5:52pm UTC](https://www.ruby-forum.com/t/regular-expression-reformating-a-value/70357/1 "2006-09-28T17:52:34Z")

</div>

I am not really sure how to do this. I have a series of number that need  
parsed. Strip the zeros, if a caret  
exists replace it with a slash.

1.) 000123^2 - should be 123/2  
2.) 1^2 - should be 1/2  
3.) 0001 - should be 1  
4.) 000123 - should be 123

I do not know how to do the condtional to exclude the slash on examples  
3  
and 4. How do I prevent the addition of the slash when there is no caret  
in  
the number?

reg = /(?:[1-9]{1}\d\*)\W(?:\d\*)/

puts reg.match(“000123^2”);

puts reg.match(“123^2”);

puts reg.match(“0001”);

puts reg.match(“000123”);

puts reg.match(“123”);

Thanks!

---

<div class="post-metadata">

**Author:** ![stevenreplica](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c0e974/32.png) [@stevenreplica](https://www.ruby-forum.com/u/stevenreplica)\
**Post date:** [September 28, 2006, 6:28pm UTC](https://www.ruby-forum.com/t/regular-expression-reformating-a-value/70357/2 "2006-09-28T18:28:49Z")

</div>

On 9/28/06, Steven K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 4.) 000123 - should be 123  
> How about  
> s.sub!(/^\s_0+/,“”).sub(/^/,“/”)  
> than  
> if you need checking  
> %r{^\s_[0-9/]+} === s  
> which is not completely strict because of  
> 5.) 12^34/5

Cheers  
Robert

> 

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![stevenreplica](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c0e974/32.png) [@stevenreplica](https://www.ruby-forum.com/u/stevenreplica)\
**Post date:** [September 28, 2006, 6:54pm UTC](https://www.ruby-forum.com/t/regular-expression-reformating-a-value/70357/3 "2006-09-28T18:54:59Z")

</div>

What I am looking for is a single regex to accomplish this so I can  
avoid  
the parsing afterwards.

I would like to break the string into groups then reformat the string  
with  
some regex conditional logic. Is this possible?

Thanks!

Steve

---

<div class="post-metadata">

**Author:** ![stevenreplica](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c0e974/32.png) [@stevenreplica](https://www.ruby-forum.com/u/stevenreplica)\
**Post date:** [September 29, 2006, 1:20pm UTC](https://www.ruby-forum.com/t/regular-expression-reformating-a-value/70357/4 "2006-09-29T13:20:04Z")

</div>

Perfect!

Thanks!

Steve

---

<div class="post-metadata">

**Author:** ![stevenreplica](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c0e974/32.png) [@stevenreplica](https://www.ruby-forum.com/u/stevenreplica)\
**Post date:** [September 29, 2006, 12:02am UTC](https://www.ruby-forum.com/t/regular-expression-reformating-a-value/70357/5 "2006-09-29T00:02:05Z")

</div>

On Fri, 2006-09-29 at 00:51 +0900, Steven K. wrote:

> I am not really sure how to do this. I have a series of number that need  
> parsed. Strip the zeros, if a caret  
> exists replace it with a slash.
> 
> 1.) 000123^2 - should be 123/2  
> 2.) 1^2 - should be 1/2  
> 3.) 0001 - should be 1  
> 4.) 000123 - should be 123

Only lightly tested, but does this do what you’re after?

“000123^2”.gsub(/(^0+)?(\d\*)(^)?/) { “#{$2}#{’/’ if $3}” }

# =\> “123/2”

“1^2”.gsub(/(^0+)?(\d\*)(^)?/) { “#{$2}#{’/’ if $3}” }

# =\> “1/2”

“0001”.gsub(/(^0+)?(\d\*)(^)?/) { “#{$2}#{’/’ if $3}” }

# =\> “1”

“000123”.gsub(/(^0+)?(\d\*)(^)?/) { “#{$2}#{’/’ if $3}” }

# =\> “123”

I’m assuming you mean only leading zeroes, e.g:

“0001^0001^1”.gsub(/(^0+)?(\d\*)(^)?/) { “#{$2}#{’/’ if $3}” }

# =\> “1/0001/1”

If that’s not what you want, I think you could strip all the leading  
zeroes with a small change:

“0001^0001^1”.gsub(/(0+)?(\d\*)(^)?/) { “#{$2}#{’/’ if $3}” }

# =\> “1/1/1”

Hope that helps,
