Ruby regular expression using gsub?

You can simply remove 0s in parts that ends with a slash Works for me require "date" class Date def m_d_y strftime('%m/%d/%Y'). Gsub(/0(\d)\//, "\\1/") end end puts Date. Civil(1980, 1, 1).

M_d_y puts Date. Civil(1980, 10, 1). M_d_y puts Date.

Civil(1980, 1, 10). M_d_y puts Date. Civil(1908, 1, 1).

M_d_y puts Date. Civil(1908, 10, 1). M_d_y puts Date.

Civil(1908, 1, 10). M_d_y outputs 1/1/1980 10/1/1980 1/10/1980 1/1/1908 10/1/1908 1/10/1908.

You can simply remove 0s in parts that ends with a slash. Works for me require "date" class Date def m_d_y strftime('%m/%d/%Y'). Gsub(/0(\d)\//, "\\1/") end end puts Date.

Civil(1980, 1, 1). M_d_y puts Date. Civil(1980, 10, 1).

M_d_y puts Date. Civil(1980, 1, 10). M_d_y puts Date.

Civil(1908, 1, 1). M_d_y puts Date. Civil(1908, 10, 1).

M_d_y puts Date. Civil(1908, 1, 10). M_d_y outputs 1/1/1980 10/1/1980 1/10/1980 1/1/1908 10/1/1908 1/10/1908.

This actually works, lol, thanks Vincent – Alex Baranosky Feb 6 '10 at 8:59 A minor variant also works nicely: strftime('%m/%d/%Y'). Gsub(/0(\d{1})\//, "\\1/") – Alex Baranosky Feb 6 '10 at 9:23 Yes, Alex. The default counter is {1} so it is not necessary, except for clarity, also "*" is equivalent to {0,} and "+" equivalent to {1,} – Vincent Robert Feb 6 '10 at 9:50 Or using positive look-ahead, you can do strftime('%m/%d/%Y').

Gsub(/0(\d)(?=\/)/, '\1'). – kejadlen Feb 6 '10 at 15:40.

02/02/1980". Gsub(/\b0/, '') #=> "2/2/1980" \b is a zero-width marker for a word boundary, therefore \b0 cannot have a digit before the zero.

Require "date" class Date def m_d_y mon, mday, year. Join("/") end end.

Nice, thanks... – Alex Baranosky Feb 6 '10 at 19:02.

AFAIK (and according regular-expressions.Info/lookaround. Html) Ruby does not support look-behinds and replacement-interpolation is done through '\1' (including quotes! ) instead of $1.

– Bart Kiers Feb 6 '10 at 18:55 I am not sure with other ruby, but ruby 1.9 support that. – YOU Feb 7 '10 at 3:10.

The problem is that it won't match valid dates so your replacement will mangle valid strings. To fix: Regex: (^|(?

– Alex Baranosky Feb 6 '10 at 8:52 Ruby does not support look-behinds. See: regular-expressions.Info/lookaround. Html – Bart Kiers Feb 6 '10 at 18:37.

You say that Ruby is throwing a syntax error, so your problem lies before you have even reached the regexp. Probably because you aren't calling strftime on anything. Try: def m_d_y t = Time.

Now t. Strftime('%m/%d/%Y'). Gsub(/0?(\d{1})\/0?(\d{1})\//, $1 + "/" + $2 + "/" ) end Then replace Time.

Now with a real time, then debug your regexp.

I was calling strftime from within the Date class :) – Alex Baranosky Feb 6 '10 at 9:22 Well then. Not sure where the downvote came from, but whatever. – Ben Feb 6 '10 at 10:07.

Instead of using a class, you can also use a list of permitted values. False as values. Validation procedure is required.

Only accepts values which match the given regular expression. There is also support for default values. Configuration parameter to configure a default value for a certain property.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions