Regular expressions

I have string as "12345678", Anybody assist me to convert this string
as "12-34-5678" and "1234-56-78". yes this is exactly Date format.

···

--
Posted via http://www.ruby-forum.com/.

text = “12345678”

puts "#{$1}-#{$2}-#{$3}" if text =~ /\A(\d{4})(\d{2})(\d{2})\z/
puts "#{$1}-#{$2}-#{$3}" if text =~ /\A(\d{2})(\d{2})(\d{4})\z/

Also:
puts "#{$3}-#{$2}-#{$1}" if text =~ /\A(\d{2})(\d{2})(\d{4})\z/

The $1, $2, and $3 correspond to the captured matches (the patterns between parentheses) in order of appearance within the full regular expression.

HTH,
Ammar

···

On Dec 26, 2013, at 9:37 AM, Selvag Ruby <lists@ruby-forum.com> wrote:

I have string as "12345678", Anybody assist me to convert this string
as "12-34-5678" and "1234-56-78". yes this is exactly Date format.

What do you want to do with it subsequently? If it is a date it
should be parsed as a Date.

Kind regards

robert

···

On Thu, Dec 26, 2013 at 8:37 AM, Selvag Ruby <lists@ruby-forum.com> wrote:

I have string as "12345678", Anybody assist me to convert this string
as "12-34-5678" and "1234-56-78". yes this is exactly Date format.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Hi Robert, I am getting the numbers within quotes and need convert this
to date format to store in DB.

···

--
Posted via http://www.ruby-forum.com/.

My turn:

text="12345678"
p "%s%s-%s-%s" % text.scan(/\d{2}/) if text[/\d{8}/] # => *"*1234-56-78*"*
p "%s-%s-%s%s" % text.scan(/\d{2}/) if text[/\d{8}/] # => *"*12-34-5678"

···

On Thu, Dec 26, 2013 at 2:50 AM, Ammar Ali <ammarabuali@gmail.com> wrote:

On Dec 26, 2013, at 9:37 AM, Selvag Ruby <lists@ruby-forum.com> wrote:

> I have string as "12345678", Anybody assist me to convert this string
> as "12-34-5678" and "1234-56-78". yes this is exactly Date format.

text = “12345678”

puts "#{$1}-#{$2}-#{$3}" if text =~ /\A(\d{4})(\d{2})(\d{2})\z/
puts "#{$1}-#{$2}-#{$3}" if text =~ /\A(\d{2})(\d{2})(\d{4})\z/

Also:
puts "#{$3}-#{$2}-#{$1}" if text =~ /\A(\d{2})(\d{2})(\d{4})\z/

The $1, $2, and $3 correspond to the captured matches (the patterns
between parentheses) in order of appearance within the full regular
expression.

HTH,
Ammar

Then it seems even more appropriate to use class Date instead of a
String representation. Usually DB drivers support handling the data
with proper types. Or is the field in the database of type VARCHAR?
That would be bad design IMHO.

Kind regards

robert

···

On Fri, Dec 27, 2013 at 5:29 AM, Selvag Ruby <lists@ruby-forum.com> wrote:

Hi Robert, I am getting the numbers within quotes and need convert this
to date format to store in DB.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

you can also pass a block to match:

    text.match(/\A(\d{4})(\d{2})(\d{2})\z/) { puts "#{$1}-#{$2}-#{$3}" }

Alternatively, if you are looking to do some work with those dates:

    require 'date'
    Date.strptime("20131227", "%Y%m%d")

While it eats a bit more resource, this has the nice side effect of tossing
you an error if the data you are parsing does not generate a valid date
from rubyland, instead of relying on whatever backend database you are
using to perform the validation as it casts the formatted string into a
timestamp representation, etc.

randym

···

On Fri, Dec 27, 2013 at 1:29 PM, Selvag Ruby <lists@ruby-forum.com> wrote:

Hi Robert, I am getting the numbers within quotes and need convert this
to date format to store in DB.

--
Posted via http://www.ruby-forum.com/\.