Michael
Michael

Reputation: 573

What's wrong with my regex in ruby?

I am writing a small ruby program that needs to extract time and dates from a string given to it... It will eventually make a guess as to what type of appointment/meeting it is and use it to best serve the user. Unfortunately the below regex works in my sublime text regular expression search but not in ruby.

email = "
Med Check
From Google Calendar
This invitation is out of date. This event has been updated.
View updated information on Google Calendar
more details »
Med Check
When
Mon Jan 10, 2013 9:30am – 10:30am Eastern Time
Calendar
Going?   Yes - Maybe - No    more options »".downcase;

require 'time'


#January    February    March   April   May June    July    August  September   October November    December
r = /(jan(|uary)|feb(|uary)|mar(|ch)|apr(|il)|may|jun(|e)|jul(|y)|aug(|ust)|sept(|tember)|oct(|ober|)|nov(|ember)|dec(|ember)|(\b([1-9]|[12][0-9]|3[01])\b))( |/|,|)(([0-3]|)[0-9]|)(, |\/| )\b2[0-9]{3}\b/
if email[r]
  puts email[r]
  date =Date.parse(email[r])
  puts " We found a date.. Let's see if we can find a time: #{date}"

  if date< Date.today
    puts "Why do we need to worry about this?"
  else
    r = /([0-9]|)[0-9]:[0-9][0-9][ |pm|am|](am|pm)/
    if email[r]
        time = Time.parse("#{email[r]} #{date}")
        puts "Found time #{time}"
        if time<Time.now
            puts "Error: Time before #{Time.now}"
        else
            #Great!
            puts "Finished let's add it."

        end
    end
  end
end

Throws these errors:

/Users/michael/Downloads/parse.rb:27: end pattern with unmatched parenthesis: /(jan(|uary)|feb(|uary)|mar(|ch)|apr(|il)|may|jun(|e)|jul(|y)|aug(|ust)|sept(|tember)|oct(|ober|)|nov(|ember)|dec(|ember)|(\b([1-9]|[12][0-9]|3[01])\b))( |/
/Users/michael/Downloads/parse.rb:27: syntax error, unexpected ','
...1-9]|[12][0-9]|3[01])\b))( |/|,|)(([0-3]|)[0-9]|)(, |\/| )\b...
...                               ^
/Users/michael/Downloads/parse.rb:27: syntax error, unexpected ')'
...-9]|3[01])\b))( |/|,|)(([0-3]|)[0-9]|)(, |\/| )\b2[0-9]{3}\b/
...                               ^
/Users/michael/Downloads/parse.rb:27: syntax error, unexpected ']', expecting ')'
...[01])\b))( |/|,|)(([0-3]|)[0-9]|)(, |\/| )\b2[0-9]{3}\b/
...                               ^
/Users/michael/Downloads/parse.rb:49: syntax error, unexpected end-of-input, expecting ')'

Upvotes: 0

Views: 2219

Answers (3)

linguanerd
linguanerd

Reputation: 731

Change this: (, |/| ) to this (, |\/| )

The unescaped slash is causing the ruby regex compiler to think that you have terminated the regular expression early. Adding the backslash before the slash causes it to be treated as a normal slash within the regular expression, rather than being interpreted as a delimiter indicating the end of the regular expression.

Upvotes: 1

user2141046
user2141046

Reputation: 902

Change / to \/.

Meaning, before you write forward or back-slashes in your regex, place an additional backslash (\) before them.

Upvotes: 1

Peter Alfvin
Peter Alfvin

Reputation: 29399

You have an unescaped forward slash character in your regex. You can use Rubular to test your expressions, as in http://rubular.com/r/TvoKOsSImt

Upvotes: 5

Related Questions