views:

87

answers:

2

Hi, 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

+1  A: 

Match

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

with

$1
codaddict
Thanks guys I tried, but still getting the same 1 - 55 of 55 , below is the pipe URL plz checkhttp://pipes.yahoo.com/pipes/pipe.info?_id=f2caf57c625f0030dd6f5bd412ef5b17
Balaji
+1  A: 

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
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
@Balaji: see my latest revision. I tried the new pattern and it works. http://pipes.yahoo.com/pipes/pipe.info?_id=011b2556313cb14f1690cfd13e1f3fb3
polygenelubricants
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
@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
@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
Thanks, I tried \d+ in the last it gave 20 instead of 149here is the pipe http://pipes.yahoo.com/pipes/pipe.info?_id=0ab08d7739a057a374d9c42e1472212f
Balaji
oh sorry I missed the brackets, Thanks a lot, u made my day :) bye
Balaji
@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