regualr expression (need help)

Discussion in 'Ruby' started by Heinrich Piard, Jan 13, 2008.

  1. Hi all,

    anybody an idea how to transform the date out of this string
    Not After : Apr 3 11:13:11 2008 GMT

    to

    2008-4-3


    Help is very apreciated!!

    Thx.

    Henry
    --
    Posted via http://www.ruby-forum.com/.
    Heinrich Piard, Jan 13, 2008
    #1
    1. Advertising

  2. Heinrich Piard wrote:
    > anybody an idea how to transform the date out of this string
    > Not After : Apr 3 11:13:11 2008 GMT
    >
    > to
    >
    > 2008-4-3


    Time.parse("Not After : Apr 3 11:13:11 2008 GMT") will give you a Time
    object. You can get the desired string from that quite easily (using strftime
    for example, or just using Time#day, Time#month and Time#year directly).

    HTH,
    Sebastian
    --
    Jabber:
    ICQ: 205544826
    Sebastian Hungerecker, Jan 13, 2008
    #2
    1. Advertising

  3. [Note: parts of this message were removed to make it a legal post.]

    I would create a time object and then format it how you like.

    Time.parse(string).strftime(format_string)

    Look up the docs for Time#strftime to determine the format string.

    On Jan 13, 2008 12:04 PM, Heinrich Piard <> wrote:

    > Hi all,
    >
    > anybody an idea how to transform the date out of this string
    > Not After : Apr 3 11:13:11 2008 GMT
    >
    > to
    >
    > 2008-4-3
    >
    >
    > Help is very apreciated!!
    >
    > Thx.
    >
    > Henry
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    Daniel Finnie, Jan 13, 2008
    #3
  4. Daniel Finnie wrote:
    > I would create a time object and then format it how you like.
    >
    > Time.parse(string).strftime(format_string)
    >
    > Look up the docs for Time#strftime to determine the format string.



    Guys - thanks a lot.
    It works just great!!

    Here the snipped:
    ValidDate = File.open("someFile.txt").readlines.to_s.grep(/Not After/)
    #puts 'Certificate is valid ' + ValidDate.to_s
    CertValidDate = Time.parse(ValidDate.to_s).strftime("%Y-%d-%m")
    #puts CertValidDate

    Now I can do my comparison with actualDate and CertValidDAte and I am
    able to create an alarm.

    Thanks!!!

    bye
    Henry
    --
    Posted via http://www.ruby-forum.com/.
    Heinrich Piard, Jan 13, 2008
    #4
  5. Heinrich Piard

    louis Guest

    sorry to post what might be a moronic question, but i although
    Time.parse shows up in the online core docs, Time.parse isn't a method
    on any machine of mine.

    e.g.

    irb(main):001:0> Time.methods.sort
    => [:"!", :"!=", :"!~", :<, :<=, :<=>, :==, :===, :=~, :>, :>=,
    :__id__, :__send__, :_load, :allocate, :ancestors, :at, :autoload,
    :autoload?, :class, :class_eval, :class_exec,
    :class_variable_defined?, :class_variable_get, :class_variable_set,
    :class_variables, :clone, :const_defined?, :const_get, :const_missing,
    :const_set, :constants, :define_singleton_method, :display, :dup,
    :enum_for, :eql?, :equal?, :extend, :freeze, :frozen?, :gem, :gm,
    :hash, :include?, :included_modules, :inspect, :instance_eval,
    :instance_exec, :instance_method, :instance_methods, :instance_of?,
    :instance_variable_defined?, :instance_variable_get,
    :instance_variable_set, :instance_variables, :is_a?, :kind_of?,
    :local, :method, :method_defined?, :methods, :mktime, :module_eval,
    :module_exec, :name, :new, :nil?, :now, :eek:bject_id,
    :private_class_method, :private_instance_methods,
    :private_method_defined?, :private_methods,
    :protected_instance_methods, :protected_method_defined?,
    :protected_methods, :public_class_method, :public_instance_method,
    :public_instance_methods, :public_method, :public_method_defined?,
    :public_methods, :public_send, :remove_class_variable, :respond_to?,
    :send, :singleton_methods, :superclass, :taint, :tainted?, :tap,
    :to_enum, :to_s, :untaint, :utc]
    (from an ubuntu box running 1.9.0--but i don't get a parse on 1.8.6 on
    ubuntu or mac)

    in fact, whytheluckstiff's online ruby terminal at hobix.com *does*
    have Time#parse. and you all are using it. any ideas? where did i drop
    the ball?

    thanks in advance.



    On Jan 13, 2008 12:47 PM, Heinrich Piard <> wrote:
    > Daniel Finnie wrote:
    > > I would create a time object and then format it how you like.
    > >
    > > Time.parse(string).strftime(format_string)
    > >
    > > Look up the docs for Time#strftime to determine the format string.

    >
    >
    > Guys - thanks a lot.
    > It works just great!!
    >
    > Here the snipped:
    > ValidDate = File.open("someFile.txt").readlines.to_s.grep(/Not After/)
    > #puts 'Certificate is valid ' + ValidDate.to_s
    > CertValidDate = Time.parse(ValidDate.to_s).strftime("%Y-%d-%m")
    > #puts CertValidDate
    >
    > Now I can do my comparison with actualDate and CertValidDAte and I am
    > able to create an alarm.
    >
    > Thanks!!!
    >
    > bye
    >
    > Henry
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    louis, Jan 13, 2008
    #5
  6. louis wrote:
    > sorry to post what might be a moronic question, but i although
    > Time.parse shows up in the online core docs, Time.parse isn't a method
    > on any machine of mine.


    Try requiring time first.
    require 'time'

    HTH,
    Sebastian
    --
    NP: Arcturus - Alone
    Jabber:
    ICQ: 205544826
    Sebastian Hungerecker, Jan 13, 2008
    #6
  7. Heinrich Piard

    louis Guest

    that did help a lot! thanks! now i have to find/read more on what's in
    the core that needs 'requiring'. thanks again!

    > > sorry to post what might be a moronic question, but i although
    > > Time.parse shows up in the online core docs, Time.parse isn't a method
    > > on any machine of mine.

    >
    > Try requiring time first.
    > require 'time'
    >
    > HTH,
    > Sebastian
    > --
    louis, Jan 13, 2008
    #7
  8. [Note: parts of this message were removed to make it a legal post.]

    Henry,

    should do :

    ValidDate = File.readlines('someFile.txt').to_s.grep(/Not After/)

    instead of :

    ValidDate = File.open("someFile.txt").readlines.to_s.grep(/Not After/)

    Sebastian corrected me once... File.open without a block will leave the file
    pointer open... :)

    M

    On Jan 13, 2008 12:47 PM, Heinrich Piard <> wrote:

    > Daniel Finnie wrote:
    > > I would create a time object and then format it how you like.
    > >
    > > Time.parse(string).strftime(format_string)
    > >
    > > Look up the docs for Time#strftime to determine the format string.

    >
    >
    > Guys - thanks a lot.
    > It works just great!!
    >
    > Here the snipped:
    > ValidDate = File.open("someFile.txt").readlines.to_s.grep(/Not After/)
    > #puts 'Certificate is valid ' + ValidDate.to_s
    > CertValidDate = Time.parse(ValidDate.to_s).strftime("%Y-%d-%m")
    > #puts CertValidDate
    >
    > Now I can do my comparison with actualDate and CertValidDAte and I am
    > able to create an alarm.
    >
    > Thanks!!!
    >
    > bye
    > Henry
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    Mike McKinney, Jan 13, 2008
    #8
  9. Mike McKinney wrote:
    > should do :
    >
    > =A0 =A0 ValidDate =3D File.readlines('someFile.txt').to_s.grep(/Not After=

    /)

    Yes, and leave out the to_s, too. If you want a string use File.read instea=
    d=20
    of File.readlines. In this case you can use both, though, since String and=
    =20
    Array both have a grep method. But using readlines to get an array and then=
    =20
    converting it to string via to_s, doesn't make sense.

    HTH,
    Sebastian
    =2D-=20
    NP: Anathema - Cerulean Twilight
    Jabber:
    ICQ: 205544826
    Sebastian Hungerecker, Jan 13, 2008
    #9
  10. Sebastian Hungerecker wrote:
    > Mike McKinney wrote:
    >> should do :
    >>
    >> � � ValidDate = File.readlines('someFile.txt').to_s.grep(/Not After/)

    >
    > Yes, and leave out the to_s, too. If you want a string use File.read
    > instead
    > of File.readlines. In this case you can use both, though, since String
    > and
    > Array both have a grep method. But using readlines to get an array and
    > then
    > converting it to string via to_s, doesn't make sense.
    >
    > HTH,
    > Sebastian


    Thanks guys,

    I did include those changes in my code.

    bye
    Henry
    --
    Posted via http://www.ruby-forum.com/.
    Heinrich Piard, Jan 13, 2008
    #10
  11. Heinrich Piard wrote:
    > Sebastian Hungerecker wrote:
    >> Mike McKinney wrote:
    >>> should do :
    >>>
    >>> � � ValidDate = File.readlines('someFile.txt').to_s.grep(/Not After/)

    >>
    >> Yes, and leave out the to_s, too. If you want a string use File.read
    >> instead
    >> of File.readlines. In this case you can use both, though, since String
    >> and
    >> Array both have a grep method. But using readlines to get an array and
    >> then
    >> converting it to string via to_s, doesn't make sense.
    >>
    >> HTH,
    >> Sebastian

    >
    > Thanks guys,
    >
    > I did include those changes in my code.
    >
    > bye
    > Henry


    Hi all,

    one more thing. I saw something in this forum about 'how to calculate
    time difference' , but I can't find it anymore.

    I want to calculate the time difference (in days):

    DateDiff = CertValidDate - actualDate

    but I get this error message: undefined method `-' for
    "2008-09-10":String (NoMethodError)

    bye
    Henry

    --
    Posted via http://www.ruby-forum.com/.
    Heinrich Piard, Jan 13, 2008
    #11
  12. Heinrich Piard

    Ken Bloom Guest

    On Sun, 13 Jan 2008 14:37:35 -0500, louis wrote:

    > that did help a lot! thanks! now i have to find/read more on what's in
    > the core that needs 'requiring'. thanks again!


    I've noticed that. I think it's a bug on ruby-docs.org that somebody
    needs to fix.

    --Ken

    --
    Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
    Department of Computer Science. Illinois Institute of Technology.
    http://www.iit.edu/~kbloom1/
    Ken Bloom, Jan 14, 2008
    #12
  13. [Note: parts of this message were removed to make it a legal post.]

    na, you are trying to execute the "-" method on a string (which doesn't
    exist)

    if you have two dates, you can execute the "-" method just fine (result is a
    Rational)

    try this:
    require 'date'

    date_one = Date.parse('2008-01-01')
    date_two = Date.parse('2008-02-02')

    puts "date_one = #{date_one}"
    puts "date_two = #{date_two}"
    diff = date_two - date_one
    puts "date_two - date_one = #{diff} (class:#{diff.class})"
    puts "date_one + diff.to_i = #{date_one + diff.to_i}"

    output:
    date_one = 2008-01-01
    date_two = 2008-02-02
    date_two - date_one = 32 (class:Rational)
    date_one + diff.to_i = 2008-02-02

    M

    On Jan 13, 2008 8:41 PM, Ken Bloom <> wrote:

    > On Sun, 13 Jan 2008 14:37:35 -0500, louis wrote:
    >
    > > that did help a lot! thanks! now i have to find/read more on what's in
    > > the core that needs 'requiring'. thanks again!

    >
    > I've noticed that. I think it's a bug on ruby-docs.org that somebody
    > needs to fix.
    >
    > --Ken
    >
    > --
    > Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
    > Department of Computer Science. Illinois Institute of Technology.
    > http://www.iit.edu/~kbloom1/ <http://www.iit.edu/%7Ekbloom1/>
    >
    >
    Mike McKinney, Jan 14, 2008
    #13
  14. Heinrich Piard wrote:
    > DateDiff =3D CertValidDate - actualDate
    >
    > but I get this error message: =C2=A0 undefined method `-' for
    > "2008-09-10":String (NoMethodError)


    You need a Date object, not a String. Like this:
    Date.parse("2008-09-10") - Date.today


    HTH,
    Sebastian
    =2D-=20
    Jabber:
    ICQ: 205544826
    Sebastian Hungerecker, Jan 14, 2008
    #14
  15. Mike McKinney wrote:
    > na, you are trying to execute the "-" method on a string (which doesn't
    > exist)


    No, he's not. You're in the wrong subthread.


    --
    Jabber:
    ICQ: 205544826
    Sebastian Hungerecker, Jan 14, 2008
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,289
  2. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    846
    Alan Moore
    Dec 2, 2005
  3. GIMME
    Replies:
    3
    Views:
    11,950
    vforvikash
    Dec 29, 2008
  4. snacktime
    Replies:
    13
    Views:
    561
    Dan Sommers
    Feb 22, 2005
  5. Ken
    Replies:
    3
    Views:
    330
    ultimadj
    Feb 21, 2007
Loading...

Share This Page