Hi - I would like to use gsub() to strip decimals with trailing zeros from a string. My string looks like this: -- 19.0 " / 482.600 mm -- I would like to end up with this: -- 19 " / 482.6 mm -- Anyone have a regular expression that can do this? Thanks! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 27, 2009, at 2:47 PM, northband wrote:> > Hi - > > I would like to use gsub() to strip decimals with trailing zeros from > a string. My string looks like this: > -- > 19.0 " / 482.600 mm > -- > > I would like to end up with this: > -- > 19 " / 482.6 mm > -- > > Anyone have a regular expression that can do this? > > Thanks! > >It depends on how you make the string. "19.0".sub(/\.?0+\z/,'''') #=> "19" "482.600".sub(/\.?0+\z/,'''') #=> "482.6" If you replace \z with (\D|\z) and substitute ''\1'', it might work. (You can try it out yourself.) -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Awesome - this is a start - I''ll take it from here. Thanks! On Feb 27, 4:14 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Feb 27, 2009, at 2:47 PM, northband wrote: > > > > > > > Hi - > > > I would like to use gsub() to strip decimals with trailing zeros from > > a string. My string looks like this: > > -- > > 19.0 " / 482.600 mm > > -- > > > I would like to end up with this: > > -- > > 19 " / 482.6 mm > > -- > > > Anyone have a regular expression that can do this? > > > Thanks! > > It depends on how you make the string. > > "19.0".sub(/\.?0+\z/,'''') #=> "19" > "482.600".sub(/\.?0+\z/,'''') #=> "482.6" > > If you replace \z with (\D|\z) and substitute ''\1'', it might work. > (You can try it out yourself.) > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
string = ''10.0'' string.sub!(/\.\d+/, '''') This will replace in place (sub!) any dot (\.) followed by at least one number (\d+) with nothing (''''). Pepe On Feb 27, 4:37 pm, northband_101 <northb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Awesome - this is a start - I''ll take it from here. > > Thanks! > > On Feb 27, 4:14 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> > wrote: > > > On Feb 27, 2009, at 2:47 PM, northband wrote: > > > > Hi - > > > > I would like to use gsub() to strip decimals with trailing zeros from > > > a string. My string looks like this: > > > -- > > > 19.0 " / 482.600 mm > > > -- > > > > I would like to end up with this: > > > -- > > > 19 " / 482.6 mm > > > -- > > > > Anyone have a regular expression that can do this? > > > > Thanks! > > > It depends on how you make the string. > > > "19.0".sub(/\.?0+\z/,'''') #=> "19" > > "482.600".sub(/\.?0+\z/,'''') #=> "482.6" > > > If you replace \z with (\D|\z) and substitute ''\1'', it might work. > > (You can try it out yourself.) > > > -Rob > > > Rob Biedenharn http://agileconsultingllc.com > > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sorry, I didn''t read your first posting fully. My solution will not work for the case of 482.600. Pepe On Feb 28, 10:12 am, pepe <P...-1PhG29ZdMB/g+20BJ0uB2w@public.gmane.org> wrote:> string = ''10.0'' > string.sub!(/\.\d+/, '''') > > This will replace in place (sub!) any dot (\.) followed by at least > one number (\d+) with nothing (''''). > > Pepe > > On Feb 27, 4:37 pm, northband_101 <northb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Awesome - this is a start - I''ll take it from here. > > > Thanks! > > > On Feb 27, 4:14 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> > > wrote: > > > > On Feb 27, 2009, at 2:47 PM, northband wrote: > > > > > Hi - > > > > > I would like to use gsub() to strip decimals with trailing zeros from > > > > a string. My string looks like this: > > > > -- > > > > 19.0 " / 482.600 mm > > > > -- > > > > > I would like to end up with this: > > > > -- > > > > 19 " / 482.6 mm > > > > -- > > > > > Anyone have a regular expression that can do this? > > > > > Thanks! > > > > It depends on how you make the string. > > > > "19.0".sub(/\.?0+\z/,'''') #=> "19" > > > "482.600".sub(/\.?0+\z/,'''') #=> "482.6" > > > > If you replace \z with (\D|\z) and substitute ''\1'', it might work. > > > (You can try it out yourself.) > > > > -Rob > > > > Rob Biedenharn http://agileconsultingllc.com > > > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
OK, got something working you might be able to use. Just to make things more complicated: s = ''19.0 / 482.600 mm / 19.060 / 482.600 mm'' s.gsub!(/(\.0?[^0])?0+/, ''\1'').gsub!(/\.[\s\n]/, '''') Pepe On Feb 28, 10:13 am, pepe <P...-1PhG29ZdMB/g+20BJ0uB2w@public.gmane.org> wrote:> Sorry, I didn''t read your first posting fully. My solution will not > work for the case of 482.600. > > Pepe > > On Feb 28, 10:12 am, pepe <P...-1PhG29ZdMB/g+20BJ0uB2w@public.gmane.org> wrote: > > > string = ''10.0'' > > string.sub!(/\.\d+/, '''') > > > This will replace in place (sub!) any dot (\.) followed by at least > > one number (\d+) with nothing (''''). > > > Pepe > > > On Feb 27, 4:37 pm, northband_101 <northb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Awesome - this is a start - I''ll take it from here. > > > > Thanks! > > > > On Feb 27, 4:14 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> > > > wrote: > > > > > On Feb 27, 2009, at 2:47 PM, northband wrote: > > > > > > Hi - > > > > > > I would like to use gsub() to strip decimals with trailing zeros from > > > > > a string. My string looks like this: > > > > > -- > > > > > 19.0 " / 482.600 mm > > > > > -- > > > > > > I would like to end up with this: > > > > > -- > > > > > 19 " / 482.6 mm > > > > > -- > > > > > > Anyone have a regular expression that can do this? > > > > > > Thanks! > > > > > It depends on how you make the string. > > > > > "19.0".sub(/\.?0+\z/,'''') #=> "19" > > > > "482.600".sub(/\.?0+\z/,'''') #=> "482.6" > > > > > If you replace \z with (\D|\z) and substitute ''\1'', it might work. > > > > (You can try it out yourself.) > > > > > -Rob > > > > > Rob Biedenharn http://agileconsultingllc.com > > > > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
There appear to be some good solutions here, but I thought I''d jump in with a bit of non-Rails technical detail. I''d double check with the source of this data - the zeros may be significant. (see http://en.wikipedia.org/wiki/Significant_figures) The data given doesn''t seem to match that (482.600 mm would be written as 19.0000"), but it doesn''t hurt to verify... --Matt Jones On Feb 27, 2:47 pm, northband <northb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi - > > I would like to use gsub() to strip decimals with trailing zeros from > a string. My string looks like this: > -- > 19.0 " / 482.600 mm > -- > > I would like to end up with this: > -- > 19 " / 482.6 mm > -- > > Anyone have a regular expression that can do this? > > Thanks!--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi. I just tested the regexp against 19.0000 and it works, but I got a little problem with the [\s\n]I just solved: s = ''19.0000 / 482.600 mm / 19.060000 / 482.600 mm'' s.gsub!(/(\.0?[^0])?0+/, ''\1'').gsub!(/\.([\s\n])/, ''\1'') This produces: ''19 / 482.6 mm / 19.06 / 482.6 mm'' The "insignificant" trailing zeros after are always trimmed after a decimal point. Pepe On Feb 28, 6:30 pm, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> There appear to be some good solutions here, but I thought I''d jump > in > with a bit of non-Rails technical detail. > > I''d double check with the source of this data - the zeros may be > significant. > (seehttp://en.wikipedia.org/wiki/Significant_figures) > > The data given doesn''t seem to match that (482.600 mm would be written > as > 19.0000"), but it doesn''t hurt to verify... > > --Matt Jones > > On Feb 27, 2:47 pm, northband <northb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi - > > > I would like to use gsub() to strip decimals with trailing zeros from > > a string. My string looks like this: > > -- > > 19.0 " / 482.600 mm > > -- > > > I would like to end up with this: > > -- > > 19 " / 482.6 mm > > -- > > > Anyone have a regular expression that can do this? > > > Thanks!--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---