开发者

Ruby regexp: complex pattern

I have a number of possible patterns for title which I want to capture using Ruby regexp.

  1. Just ti开发者_如何学JAVAtle
  2. Ignore | The title to capture
  3. Ignore / The title to capture
  4. [Ignore] The title to capture

How do I put this into one regexp pattern?

This method deals with the 2nd case only:

  def self.format_title(title)
    title.match(/(?:.+)\|(.+)/).to_a.first.strip
  end


Your code can be rewritten into: title[/\|(.+)/),1].strip

And for all four cases I recommend to use gsub:

def format_title title
    title.gsub(/.+[\|\/]/,'').gsub(/^\[.+\]/,'').strip
end


Try this regular expression:

/^(?:[^|]*\||[^\/]*\/|\[[^\]]*\])?(.+)/

The optional non-capturing group (?:[^|]*\||[^\/]*\/|\[[^\]]*\]) consists of a pattern for each case:

  • [^|]*\| matches everything up to the first |,
  • [^\/]*\/ matches everything up to the first /, and
  • \[[^\]]*\] matches everything between [ and the first following ] at the begin of the string
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜