2013-07-14 51 views
0

我在寫一個小型的ruby程序,需要從提供給它的字符串中提取時間和日期......它最終會猜測它是什麼類型的約會/會議,並用它來最好地爲用戶服務。不幸的是,下面的正則表達式在我的崇高文本正則表達式搜索中起作用,但不在紅寶石中。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 

拋出這些錯誤:

/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 ')' 

回答

1

更改/\/

意思是說,在你寫正向表達式或反斜槓之前,在它們前面加一個額外的反斜槓(\)。

1

更改此:(, |/|)這個(, |\/|)

的轉義斜槓是造成紅寶石正則表達式編譯器認爲你已經提前終止的正則表達式。在斜槓之前添加反斜槓使其在正則表達式中被視爲正常斜槓,而不是被解釋爲指示正則表達式結束的分隔符。

+0

這是一個非常規的正規斜線。它帶*反斜槓*。 – millimoose

+0

@millimoose - 是的,當然,謝謝你的指出......修正並澄清了我的答案。 – linguanerd

相關問題