Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7897197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:06:06+00:00 2026-06-03T08:06:06+00:00

I am trying to parse through a file and replace some days/dates. For example,

  • 0

I am trying to parse through a file and replace some days/dates.

For example,
I want to change

In a post on the band's blog last night (06.05.12) 
to 
In a post on the band's blog sunday night

I am trying to use gsub! to do so.

r.gsub!(/\blast night \(([0-3][0-9]\.[0-1][0-9]\.[0-9][0-9])\)/ei,Date.strptime('\1',"%d.%m.%y").strftime("%A").to_s + ' night')

always says invalid date, but

r.gsub!(/\blast night \(([0-3][0-9]\.[0-1][0-9]\.[0-9][0-9])\)/ei,'\1')

shows the right date as 06.05.12
and

mydate = '06.05.12'
r.gsub!(/\blast night \(([0-3][0-9]\.[0-1][0-9]\.[0-9][0-9])\)/ei,Date.strptime(mydate,"%d.%m.%y").strftime("%A").to_s + ' night')

gives me the appropriate response. Why doesn’t replacing mydate with \1 work when using Date.strptime? Any suggestions on how to go around this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:06:07+00:00Added an answer on June 3, 2026 at 8:06 am

    You seem to try to reference the match group in your date function. This doesn’t work though. The syntax is only available if you replace simple strings. The gsub function replaces all references in the passed string, but only when it is actually passed to the function. Your code is equivalent to

    replacement = Date.strptime('\1',"%d.%m.%y").strftime("%A").to_s + ' night'
    r.gsub!(/\blast night \(([0-3][0-9]\.[0-1][0-9]\.[0-9][0-9])\)/ei, replacement)
    

    The “replacement” thus can’t work as '\1' is not a valid date. The gsub replacement would only applied to the string returned by your strftime function. However, you can use the magic match variables which are set the the matching groups automatically:

    r.gsub!(/\blast night \(([0-3][0-9]\.[0-1][0-9]\.[0-9][0-9])\)/ei) {
      Date.strptime($1,"%d.%m.%y").strftime("%A").to_s + ' night'
    }
    

    Notice that I wrote $1 instead of \1 in the strptime arguments

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to fetch and parse an XML-file through JavaScript. I don't control the
I'm trying to parse through fixed-width formatted file extracting x,y values of points from
I'm trying to parse through a ppm file, but first need to verify if
I want to parse an XML file from URL using JDOM. But when trying
I am trying to parse a wikitext file received through Wikipedia's API and the
I'm trying to parse CSV files uploaded by the user through PHP, but it's
I am trying to parse the below XML in to the Entity through Linq,
I'm trying parse the follow XML file: <root>Root <pai>Pai_1 <filho>Pai1,Filho1</filho> <filho>Pai1,Filho2</filho> </pai> <pai>Pai_2 <filho>Pai2,Filho1</filho>
Trying to parse an HTML document and extract some elements (any links to text
Trying to parse some XML but apparently this is too much for a lazy

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.