开发者

Get random text between two regexen

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜