开发者

Get random text between two regexen

开发者 https://www.devze.com 2023-02-22 20:08 出处:网络
I have a file of scraped posts that looks like this: 1 Name: foo Hello, my name is foo. 开发者_Go百科

I have a file of scraped posts that looks like this:

1 Name: foo

Hello, my name is foo.
开发者_Go百科
2 Name: bar

Hello.
Multi
line
post.

3 Name: foo

Hello, bar!

I am capable of writing a regex that will match just the headers (because they have some HTML that's not allowed in posts).

How would I get a random post from this file? Just the text, no names or such.

EDIT: Note that some post numbers may be missing.


If you have Ruby(1.9+)

$ ruby -0777 -ne 'a=$_.split(/\d+\sName:.*/);puts a.reject(&:empty?).shuffle.first' file


Hello, my name is foo.

$ ruby -0777 -ne 'a=$_.split(/\d+\sName:.*/);puts a.reject(&:empty?).shuffle.first' file


Hello, bar!
0

精彩评论

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