简体   繁体   English

.try在ruby中使用正则表达式

[英].try with regular expression in ruby

I have a method which returns the title of a page. 我有一个返回页面标题的方法。 The title can return string containing Episodes info and without episodes info. 标题可以返回包含剧集信息但不包含剧集信息的字符串。 For eg: "Friends Can chat" and "Episode 12 Hollow man" I need to use regular expression on the final string and return it. 例如:“朋友可以聊天”和“第12集空心人”,我需要在最后一个字符串上使用正则表达式并将其返回。

title = row.at("h3")
           .try(:children)
           .try(:first)
           .try(:text)
           .try(:strip)
           .try(:[], /(Episode \d+ [–-]\s*)(.*)/,2)

Here I get Hollow man as title since it contains episodes info and the regular expression matches here. 在这里,我得到了空心男人作为标题,因为它包含情节信息,并且此处的正则表达式匹配。 But in the first case it returns nil, since "Friends can chat" does not have episode info and hence it would return nil on matching with regex. 但是在第一种情况下,它返回nil,因为“朋​​友可以聊天”没有情节信息,因此在与正则表达式匹配时返回nil。

So how can I return it efficiently. 因此,我该如何有效地将其退回。 I can use like 我可以像

title = row.at("h3")
           .try(:children)
           .try(:first)
           .try(:text)
           .try(:strip)

title = title.try(:[], /(Episode \d+ [–-]\s*)(.*)/,2) \
     if title && title.try(:[], /(Episode \d+ [–-]\s*)(.*)/,2)

Any other way I can return it efficiently. 我可以通过其他任何方式有效地返回它。

You might modify your regular expression to make “Episode” string not mandatory: 您可以修改正则表达式以使“ Episode”字符串不是必需的:

# /(Episode \d+ [–-]\s*)(.*)/
/(Episode\s+\d+\s+[–-]?\s*)?(.*))/

That will match both: 这将同时符合:

a = ["Friends Can chat", "Episode 12 Hollow man"]
a.map { |t| 
  #                  ⇓ No episode?—no worry!
  /(Episode\s+\d+\s*)?(.*)/ =~ t
  ($~[1] ? $~[1] : 'NO EPISODE ') + $~[2] 
}
# [
#  [0] "NO EPISODE Friends Can chat",
#  [1] "Episode 12 Hollow man"
# ]

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM