# Help with regex

**URL:** <https://www.ruby-forum.com/t/help-with-regex/71795>\
**Category:** Ruby\
**Created:** [October 11, 2006, 3:03pm UTC](https://www.ruby-forum.com/t/help-with-regex/71795 "2006-10-11T15:03:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [October 11, 2006, 3:03pm UTC](https://www.ruby-forum.com/t/help-with-regex/71795/1 "2006-10-11T15:03:30Z")

</div>

regex is not my cup of tea , i’ve been using them few times, only  
matching, no substitution…

I need to check a string before saving it into a decimal (5,2) column  
in DB and I actually don’t know how to start substitution :

the user is free to enter a value in any format, I must check if there  
is any decimal (if any , then 2 digits are mandatory… it’s none or 2 !)

the user can enter any string like “99’999.99” or “99,999.99” or  
“99999.99” or “99999”  
should be substitued to "99999.99’

I would like also to accept the european way (comma before decimal part)  
“99.999,99” or "99999,99’ should also be substitued to "99999.99’

so any input should be susbstitued to "99999.99’ takng in account  
the decimal part if any

joss

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [October 11, 2006, 3:19pm UTC](https://www.ruby-forum.com/t/help-with-regex/71795/2 "2006-10-11T15:19:57Z")

</div>

Hello,

My first shot at this:

def to\_nice\_number(n)  
frags = n.scan(/\d+/)  
return n if frags.size == 1 #the trivial case “9999”  
last\_part = frags.pop  
frags.join + “.” + last\_part  
end

Peter  
[http://www.rubyrailways.com](http://www.rubyrailways.com)

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [October 11, 2006, 3:38pm UTC](https://www.ruby-forum.com/t/help-with-regex/71795/3 "2006-10-11T15:38:36Z")

</div>

> I would like also to accept the european way (comma before decimal part)  
> “99.999,99” or "99999,99’ should also be substitued to "99999.99’

“European” would be difficult without having context. How can you tell  
if  
999.999 is one short of a million or one thousanth short of a thousand?

Apart from that, do you want to verify that the seperator is correctly  
placed:

66’000.95 is ok, but what about 650’00.95 or ,0001.95 ?

-tim

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [October 11, 2006, 5:02pm UTC](https://www.ruby-forum.com/t/help-with-regex/71795/4 "2006-10-11T17:02:03Z")

</div>

On 2006-10-11 15:37:03 +0200, “Tim B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> -tim
> 
> > so any input should be susbstitued to "99999.99’ takng in account  
> > the decimal part if any
> > 
> > joss

thanks for raising these cases,  
all system amounts are purely informative … (normal case is below  
9999.99)  
and my concern is being flexible w validation

in this 3 cases the only thing the system cares is the decimal part :

> 66’000.95 will be normalized to 66000.95  
> 650’00.95 will be normalized to 65000.95  
> ,0001.95 will ne normalized to 0001.95 =\> 1.95  
> being accepted by the system, the use will see on display that they are  
> not as they should be  
> and the user will correct them later on…

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [October 11, 2006, 3:46pm UTC](https://www.ruby-forum.com/t/help-with-regex/71795/5 "2006-10-11T15:46:52Z")

</div>

On 2006-10-11 15:18:05 +0200, Peter S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
said:

> Peter  
> [http://www.rubyrailways.com](http://www.rubyrailways.com)

Thanks Peter… actually I don’t have yet the ‘ruby way’… using scan  
, pop !! that’s it  
I tested in all cases and it works …

I also register your RSS

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [October 12, 2006, 10:00am UTC](https://www.ruby-forum.com/t/help-with-regex/71795/6 "2006-10-12T10:00:47Z")

</div>

On 11.10.2006 16:59, Josselin wrote:

> On 2006-10-11 15:37:03 +0200, “Tim B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:
> 
> > > I would like also to accept the european way (comma before decimal part)  
> > > “99.999,99” or "99999,99’ should also be substitued to "99999.99’
> > 
> > “European” would be difficult without having context. How can you tell if  
> > 999.999 is one short of a million or one thousanth short of a thousand?
> > 
> > Apart from that, do you want to verify that the seperator is correctly  
> > placed:

> all system amounts are purely informative … (normal case is below  
> 9999.99)  
> and my concern is being flexible w validation

In that case it is even more important that you get it crystal clear (at  
least for you) what values you will be accepting and how you will  
interpret them! And then write a set of test cases that cover every  
possible input you can imagine plus those that you cannot imagine.

> in this 3 cases the only thing the system cares is the decimal part :
> 
> > 66’000.95 will be normalized to 66000.95  
> > 650’00.95 will be normalized to 65000.95  
> > ,0001.95 will ne normalized to 0001.95 =\> 1.95  
> > being accepted by the system, the use will see on display that they are  
> > not as they should be  
> > and the user will correct them later on…

Frankly, if I were you, I would code to reject the last one. I may be  
missing something here but I do not know any notation / locale in which  
this would be legal.

It is difficult to get i18n right, as you might start to imagine. 🙂  
If you can, try to add some contextual information (e.g. user’s locale)  
and - even better - utilize some library code for this.

My 0.02EUR…

Kind regards

```
robert

```
