什么';我的ruby正则表达式怎么了?
我正在写一个小ruby程序,需要从给定的字符串中提取时间和日期。。。它最终会猜测它是什么类型的约会/会议,并使用它为用户提供最佳服务。不幸的是,下面的正则表达式在我的升华文本正则表达式搜索中有效,但在ruby中无效什么';我的ruby正则表达式怎么了?,ruby,regex,Ruby,Regex,我正在写一个小ruby程序,需要从给定的字符串中提取时间和日期。。。它最终会猜测它是什么类型的约会/会议,并使用它为用户提供最佳服务。不幸的是,下面的正则表达式在我的升华文本正则表达式搜索中有效,但在ruby中无效 email = " Med Check From Google Calendar This invitation is out of date. This event has been updated. View updated information on Google Calen
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
您的正则表达式中有一个未转换的正斜杠字符。您可以使用Rubular测试您的表达式,如将
/
更改为\/
这意味着,在正则表达式中写前斜杠或后斜杠之前,在它们前面加上一个反斜杠(\
)。将此更改为:(,|/|)
未经scaped的斜杠导致ruby regex编译器认为您已经提前终止了正则表达式。在斜杠之前添加反斜杠会使其在正则表达式中被视为普通斜杠,而不是被解释为表示正则表达式结尾的分隔符。这是一个未经转换的正则斜杠。它用反斜杠逃逸了。@millimoose-当然,谢谢你指出这一点。。。修正并澄清了我的答案。谢谢你的帮助。。修好了。
/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 ')'