简体   繁体   中英

How do I find matching <pre> tags using a reqular expression?

I am trying to create a simple blog that has code inclosed in <pre> tags.

I want to display "read more" after the first closing </pre> tag is encountered, thus showing only the first code segment.

I need to display all text, HTML, code up to the first closing </pre> tag.

What I've come up with so far is the follow:

 /^(.*<\/pre>).*$/m

However, this matches every closing </pre> tag up to the last one encountered.

I thought something like the following would work:

 /^(.*<\/pre>{1}).*$/m

It of course does not.

I've been using Rubular .

My solution thanks to your guys help:

 require 'nokogiri'

 module PostsHelper
def readMore(post)
    doc = Nokogiri::HTML(post.message)
    intro = doc.search("div[class='intro']")
    result = Nokogiri::XML::DocumentFragment.parse(intro)
    result << link_to("Read More", post_path(post))
    result.to_html
end
 end

Basically in my editor for the blog I wrap the blog preview in div class=intro Thus, only the intro is displayed with read more added on to it.

This is not a job for regular expressions , but for a HTML/XML parser.

Using Nokogiri , this will return all <pre> blocks as HTML, making it easy for you to grab the one you want:

require 'nokogiri'

html = <<EOT
<html>
  <head></head>
  <body>
    <pre><p>block 1</p></pre>
    <pre><p>block 2</p></pre>
  </body>
</html>
EOT

doc = Nokogiri::HTML(html)
pre_blocks = doc.search('pre')
puts pre_blocks.map(&:to_html)

Which will output:

<pre><p>block 1</p></pre>
<pre><p>block 2</p></pre>

You can capture all text upto the first closing pre tag by modifying your regular expression to,

/^(.*?<\/pre>{1}).*$/m

This way you can get the matched text by,

text.match(regex)[1]

which will return only the text upto the first closing pre tag.

Reluctant matching might help in your case:

/^(.*?<\/pre>).*$/m

But it's probably not the best way to do the thing, consider using some html parser, like Nokogiri .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM