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

  • SEARCH
  • Home
  • 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 4013004
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:19:03+00:00 2026-05-20T09:19:03+00:00

Possible Duplicate: How do I make part of a regular expression optional in Ruby?

  • 0

Possible Duplicate:
How do I make part of a regular expression optional in Ruby?

I’m trying to build a regular expression with rubular to match:

On Feb 23, 2011, at 10:22 , James Bond wrote:

OR

On Feb 23, 2011, at 10:22 AM , James Bond wrote:

Here’s what I have so far, but for some reason it’s not matching? Ideas?

(On.* (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{1,2}, [12]\d{3}.* at \d{1,2}:\d{1,2} (?:AM|PM),.*wrote:)

How can I make the AM/PM text optional? Either match AM/PM or neither?

  • 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-05-20T09:19:04+00:00Added an answer on May 20, 2026 at 9:19 am

    This seems to catch the date info. I purposely captured in groups, making it easier to build a real date:

    regex = /^On (\w+ \d+, \d+), \w+ (\S+) (\w*)\s*,/
    
    [
      'On Feb 23, 2011, at 10:22 , James Bond wrote:',
      'On Feb 23, 2011, at 10:22 AM , James Bond wrote:'  
    ].each do |ary|
      ary =~ regex
      puts "#{$1} #{$2} #{$3}"
    end
    # >> Feb 23, 2011 10:22 
    # >> Feb 23, 2011 10:22 AM
    

    I purposed didn’t try to match on the months. Your sample strings look like quote headers from email messages. Those are very standard and generated by software, so you should see a lot of consistency in the format, allowing some simplification in the regex. If you can’t trust those, then go with the matches on month name abbreviations to help ignore false-positive matches. The same things apply for the day, year, and time values.

    The important thing in the regex is how to deal with the AM/PM when it’s missing.

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

Sidebar

Related Questions

Possible Duplicate: Complex CSS selector for parent of active child How would I make
Possible Duplicate: What Ruby IDE do you prefer? I've generally been doing stuff on
Possible Duplicate: How can I make a while True break if certain key is
Possible Duplicate: How to make PHP set HTTP status code to 500 automatically in
Possible Duplicate: Can you make an Extension Method Static/Shared? Extension methods are great! Pardon
Possible Duplicate: java PreparedStatement Can I make the prepared statement SELECT * FROM STUDENTS
Possible Duplicate: Why am I able to make a function call using an invalid
Possible Duplicate: Is there some ninja trick to make a variable constant after its
Possible Duplicate: Why is lock(this) {…} bad? In C# to make a critical region
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should

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.