我有一个字符串,如下所示
"The Big Bang Theory - Season 1"
我想在"- Season 1"
之前提取任何字符串,这基本上是一个节目名称。如何在ruby中使用正则表达式
答案 0 :(得分:0)
您需要使用如下所示的先行断言。低于正向前瞻断言,匹配必须后跟字符串- Season 1
.*(?=- Season 1)
示例:
> "The Big Bang Theory - Season 1".match(/.*(?=- Season 1)/)
=> #<MatchData "The Big Bang Theory ">
> "The Big Bang Theory - Season 1".match(/.*(?= - Season 1)/)
=> #<MatchData "The Big Bang Theory">
答案 1 :(得分:0)