如何在ruby中的两个字符串之间获取文本?

Abd*_*lla 3 ruby regex string parsing

我有一个包含此文本的文本文件:

What's New in this Version
==========================
-This is the text I want to get 
-It can have 1 or many lines
-These equal signs are repeated throughout the file to separate sections

Primary Category
================
Run Code Online (Sandbox Code Playgroud)

我只想获取 ========================== 和 Primary Category 之间的所有内容,并将该文本块存储在变量中。我认为以下匹配方法会起作用,但它给了我 NoMethodError: undefined method `match'

    f = File.open(metadataPath, "r")
    line = f.readlines
    whatsNew = f.match(/==========================(.*)Primary Category/m).strip
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?提前致谢。

Chr*_*nch 5

f是一个文件描述符 - 你想匹配文件中的文本,你读入line. 我更喜欢做而不是将文本读入数组(这很难正则表达式)只是将其读入一个字符串:

contents = File.open(metadataPath) { |f| f.read }
contents.match(/==========================(.*)Primary Category/m)[1].strip
Run Code Online (Sandbox Code Playgroud)

最后一行产生您想要的输出:

-This is the text I want to get \n-It can have 1 or many lines\n-These equal signs are repeated throughout the file to separate sections"
Run Code Online (Sandbox Code Playgroud)