user984621
user984621

Reputation: 48453

Ruby - remove part of the text

I have a text similar to this:

<p>some text ...</p><p>The post <a href="http://url_address/">text...</a> appeared first on <a href="http://url_address">some another text</a>.</p>

I need to remove everything from <p>The post, so the results would be:

<p>some text ...</p>

I am trying ot do that this way:

text.sub!(/^<p>The post/, '')

But it returns just an empty string... how to fix that?

Upvotes: 1

Views: 136

Answers (3)

M&#225;t&#233;
M&#225;t&#233;

Reputation: 2345

'^' is matching the beginning of the whole string. try doing

text.sub!(/<p>The post/, '')

EDIT just read it more carefully...

text.sub!(/<p>The post.*$/, '')

Upvotes: 2

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230346

Your regex is incorrect. It matches every <p>The post that is in the beginning of the string. You want the opposite: match from its position to the end of the string. Check this out.

s = '<p>some text ...</p><p>The post <a href="http://url_address/">text...</a> appeared first on <a href="http://url_address">some another text</a>.</p>'

s.sub(/<p>The\spost.*$/, '') # => "<p>some text ...</p>"

Upvotes: 4

Pablo Fernandez heelhook
Pablo Fernandez heelhook

Reputation: 12503

You have specified ^, which matches the beginning of a string. You should do

text.sub!(/<p>The post.*$/, '')

Play with this in http://rubular.com/r/c91EbHN0Af

Upvotes: 2

Related Questions