Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8049269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:44:43+00:00 2026-06-05T06:44:43+00:00

I’ve struggling to try to do this with a simple regex but it’s never

  • 0

I’ve struggling to try to do this with a simple regex but it’s never been very accurate. It doesn’t have to be perfect.

Source has a combination of
and

tags. I don’t want to count blank lines.

Old way:

  self.words = rendered.gsub(/<p>&nbsp;<\/p>/,'').gsub(/<p><br\s?\/?>|(?:<br\s?\/?>){2,}/,'<br>').scan(/<br>|<br \/>|<p/).size+1

New way (not working:
Tries to turn all the
+ into paragraphs, then throw it into nokogiri to count paragraph tags with more than 3 chars in them (I have no idea how? Counting 1 letter lines would be nice too, but this worked ok in javascript)

  h = rendered
  h.gsub!(/<br>\s*<br>/gi,"<p>")
  h.gsub!(/<br>/gi,"<p>") if h =~ /<br>\s*<br>/
  h.prepend "<p>" if !h =~ /^\s*<p[^>]*>/i
  h.replace(/<p>\s*<p>/g,"<p>&nbsp;</p><p>")
  Nokogiri::HTML(rendered)
  # find+count p tags with at least 1-3 chars?

  # this is javascript not ruby, but you get the idea
  $('p', c).each(function(i) { // had to trim it to remove whitespaces from start/end.
    if ($(this).children('img').length) return; // skip if it's just an image.
    if ($.trim($(this).text()).length > 3)
      $(this).append("<div class='num'>"+ (n += 1) +"</div>");
  })

Other methods are welcome!

Example poem ( http://allpoetry.com/poem/7429983-the_many_endings-by-Kevin )

<p>
    from the other side of silence<br>
    you met me with change and a pocket<br>
    of unhappy apples.</p>
<p>
     </p>
<p>
    <br>
    we bled together to black<br>
    and chose the path carefully to<br>
    france.<br><br>
    sometimes when you smile<br>
    your radiant footsteps fall<br>
    and all around us is silence:<br>
    each dream step is<br>
    false but full of such glory</p>
<p>
     </p>
<p>
    <br>
    unhappiness never made a student of you:<br>
    just two by two by two.  now three<br>
    this great we that overflows our<br>
    heart-cave<br><br>
    each jewel-like addition to the delicate<br>
    crown.  but flowers fall and dreams,<br>
    all dreams, come to and end with death.</p> 

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:44:44+00:00Added an answer on June 5, 2026 at 6:44 am

    For posterity, here’s what I’m using now and it seems to be quite accurate. Non latin chars cause some problems sometimes from ckeditor, so I’m stripping them out for now.

      html = Nokogiri::HTML(rendered)
      text = html.at('body').inner_text rescue nil
      return self.words = rendered.gsub(/<p>&nbsp;<\/p>/,'').gsub(/<p><br\s?\/?>|(?:<br\s?\/?>){2,}/,'<br>').scan(/<br>|<br \/>|<p/).size+1 if !text
    
      #bonus points to strip lines entirely non-letter. idk
    
      #d "text is", text.gsub!(/([\x09|\x0D|\t])|(\xc2\xa0){1,}|[^A-z]/u,'')
      text.gsub!(/[^A-z\n]/u,'')
      #d "text is", text
      self.words = text.strip.scan(/(\s*\n\s*)+/).size+1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.