我的计划:
List<Application> findAllByMyKey_Offer(String jobTitle);
}
之后和该行结尾的所有内容。Send to:
至该行结尾之间的所有内容。注意:Attn:
行可以是可选的。在这种情况下,只需返回第一行。
字符串如下:
Attn
期望的返回值必须是:
str = <<-MSG
Registry of Credit Recommendations
American Council on Education
One Dupont Circle, NW
Washington, D.C. 20036
Transcript Print Date: 10/03/2018
Sent By:Send To: American University
4400 Massachusetts Avenue, NW
Washington, DC 20016-8001
Attn: Undergraduate Admissions
Jonathan A Jones
30 People's Court
Second Address Line
Third Address Line
Augusta, GA 30909
MSG
**请注意,必须包括“收件人:”部分,而不仅仅是其内容。 **
这是我的方法,仅适用于Attn部分,但我不知道如何获得“ American University”部分。
American University
Attn: Undergraduate Admissions
测试:http://rubular.com/r/Px4ru6WrAg
感谢您的帮助。
答案 0 :(得分:1)
您可以使用alternation
(?<=Send To:).*|Attn:.*
(?<=Send To:)
,肯定左边的是Send To:
。然后将任意字符匹配一次或多次|
或Attn:.+
匹配Attn:后跟任意字符一次或多次答案 1 :(得分:0)
请注意,您不必使用正则表达式。
str.each_line.
map do |line|
case
when line.include?("Send To: ")
line[line.index("Send To: ") + "Send To: ".size..-2]
when line.include?("Attn: ")
line[line.index("Attn: ")..-2]
else
nil
end
end.compact
#=> ["American University", "Attn: Undergraduate Admissions"]
-2
排除每行末尾的换行符。