help with regex

J

Josselin

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
 
P

Peter Szinek

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
 
T

Tim Becker

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
 
J

Josselin

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

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
 
J

Josselin

"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

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

Robert Klemme

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

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,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top