How to Use Regex to Ensure Complete Words While Adding a Character Limit to Yahoo Pipes?

964 Views Asked by At

I'm pretty new to this, so excuse me if my question isn't that clear. I'm pulling an RSS Feed into Yahoo Pipes and using Regex to modify it. Here's what I'm trying to do:

  1. Limit the number of characters in an entry, but...
  2. Make sure the item includes complete words, and...
  3. If the item is shortened, add an ellipses, but...
  4. If it falls within the limits nothing should be done to it

So, if a feed's Title is: "This article is important" and the limit is 20 characters, the result should be "This article is..." But if the Title is "Good Article," nothing should happen to it.

After doing some research I think that I want to combine an if/then statement with lookahead, i.e. go to the character limit and if there is a character following it that is a space, add an ellipses, if it is a number or letter, go to the final space within the limit and add an ellipses, but if there isn't any character following it, don't do anything. Does this make sense? Is there an easier way to do what I'm going for?

I would really appreciate any help you could provide. Thanks!

1

There are 1 best solutions below

2
On BEST ANSWER

Try replacing the title using the following pattern:

^(?=.{23})(.{0,20})(?=\s).*$

With the string

$1...

Working example: http://pipes.yahoo.com/pipes/pipe.info?_id=04158a7a5ea390b1b0b78ebccadcec79

How does it work?

  • (?=.{23}) - First, we check the length is at least 23 (that's for 20 + '...', you can play with that)
  • (.{0,20}) - Match at most 20 characters on the first group.
  • (?=\s) - Make sure there's a space after the last character. If not, it will try to match fewer characters.
  • .* - Match all the way to the end, so the rest of the line is removed.

An edge case here is a single word longer than 20 characters. If that's a problem, you can solve it by using:

^(?=.{23})(.{0,20}(?=\s)|\S{20}).*$