3

I want to know what regular expression should be applied to replace 1 - 55 of 55 to only get 55 in Regex module of yahoo pipes.

Thanks

polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
Balaji
  • 31
  • 1
  • 4

2 Answers2

1

Match

\d+ - (\d+) of \1 

with

$1
codaddict
  • 445,704
  • 82
  • 492
  • 529
  • Thanks guys I tried, but still getting the same 1 - 55 of 55 , below is the pipe URL plz check http://pipes.yahoo.com/pipes/pipe.info?_id=f2caf57c625f0030dd6f5bd412ef5b17 – Balaji Aug 25 '10 at 11:55
1

You can try to match this:

\d+ - (\d+) of \1

And replace with $1, which is what group 1 captured.

The \d is the digit character class, + is one-or-more repetition. The (…) is a capturing group, and the \1 refers back to what that group matches. So this will match strings like:

num1 - num2 of num2
        |        |
        \________/ must match

References


Variation

This pattern is a slight modification that is more flexible in its whitespace matching:

\d+\s+-\s+(\d+)\s+of\s+\1

It's similar to the previous pattern, but wherever we had just a literal space character before, we now use \s+, which is a pattern that matches a non-empty sequence of any number of whitespace characters. This includes newlines, tabs, etc.

If the third number doesn't have to be the same as the second number, then simply use another \d+ instead of \1.

\d+\s+-\s+\d+\s+of\s+(\d+)

Now this will match strings like "1 - 20 of 149", being liberal with the spacing. The bracket is now moved to match the third number, so if the entire string is to be replaced by that number (149 in this case), simply replace with $1.

If you want to capture all 3 numbers individually, you can write something like this:

(\d+)\s+-\s+(\d+)\s+of\s+(\d+)
\___/       \___/        \___/
  1           2            3

Now the first number is captured by group 1, second number by group 2, and third number by group 3.

polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • Thanks I tried, but still getting the same 1 - 55 of 55 , below is the pipe URL plz check http://pipes.yahoo.com/pipes/pipe.info?_id=f2caf57c625f0030dd6f5bd412ef5b17 – Balaji Aug 25 '10 at 12:02
  • @Balaji: see my latest revision. I tried the new pattern and it works. http://pipes.yahoo.com/pipes/pipe.info?_id=011b2556313cb14f1690cfd13e1f3fb3 – polygenelubricants Aug 25 '10 at 12:07
  • 1
    Great it worked, Thanks a Million, but what if its 1 - 20 of 149, now how can I extract just 149, in this case num1 and num2 doesn't match – Balaji Aug 25 '10 at 12:15
  • @Balaji: the problem is that the spaces in the string is not actually just a simple space. You can try matching `.*` and see that it doesn't capture the whole string. There are some newlines in there, but it's not rendered as such by the viewer. Using `\s+` takes care of that problem. – polygenelubricants Aug 25 '10 at 12:15
  • @Balaji: if the third number doesn't have to be a match, then instead of referring back to the second number by using `\1`, just use `\d+` again. See latest revision. – polygenelubricants Aug 25 '10 at 12:16
  • Thanks, I tried \d+ in the last it gave 20 instead of 149 here is the pipe http://pipes.yahoo.com/pipes/pipe.info?_id=0ab08d7739a057a374d9c42e1472212f – Balaji Aug 25 '10 at 12:25
  • 1
    oh sorry I missed the brackets, Thanks a lot, u made my day :) bye – Balaji Aug 25 '10 at 12:27
  • @Balaji: Don't forget to upvote and accept an answer that helps. That's the arrows to the left of my answer. It gives people incentives to help others. – polygenelubricants Aug 25 '10 at 12:27