1

I have a string like this:

This is a link [[abcd 1234|xyz 1234]]  [[India]] [[abcd 1234|xyz 1234]]

and I want to get :

This is a link abcd 1234 [[India]] abcd 1234

I want to take double square brackets having | and take out things that are before | and replace it with whole double square bracket thing and not replace any double square bracket not having | using Boost Regex.

Any help will be appreciated.

psyche
  • 443
  • 1
  • 5
  • 14
  • 1. `I want to take double square brackets having | and take out things that are before | and replace it with whole double square bracket thing and not replace any double square bracket not having | using Boost Regex.` This sentence makes no sense... 2. I think regexs are probably not the way to go here, they're not good for matching up opening/closing brackets. – BoBTFish Dec 18 '12 at 14:31
  • With regexs I always wonder if there is a better way to do it. And I was bored, so I came up with a way just using `std::string` facilities. I'll let you decide if it's better or not, but [here it is](http://ideone.com/hJyhJ4). – BoBTFish Dec 18 '12 at 15:36

2 Answers2

1

Just use the look-ahead, (?!pattern)

#include <iostream>
#include <string>
#include <boost/regex.hpp>

int main()
{
    std::string str = "This is a link [[abcd 1234|xyz 1234]]  [[India]] [[abcd 1234|xyz 1234]]";
    boost::regex re("\\[\\[(((?!\\]\\]).)+)\\|.*?]]");
    std::cout << boost::regex_replace(str, re, "$1") << '\n';
}

demo: http://liveworkspace.org/code/2Mu5cN

Cubbi
  • 46,567
  • 13
  • 103
  • 169
  • Thanks you made my day can you similarly provide regex for getting xyz 1234 in place of abcd 1234 in string?? – psyche Dec 18 '12 at 16:36
  • @PragneshPatel No tricks there, just capture (surround with parentheses) the `.*?` after the `|` and change the number in `$1` (to `$3` in this case) – Cubbi Dec 18 '12 at 16:56
  • Sorry to again take your time can you tell how to just extract those values using boost if no replacement is required question is what to do with regex – psyche Dec 18 '12 at 17:15
  • @PragneshPatel To extract individual submatches, I would use `regex_token_iterator` – Cubbi Dec 18 '12 at 18:05
0
"\\[\\[(.*?)\|.*?]]"

This will match text between [[ and ]] with a | separator. $1 is the text between the [[ and the |.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • @PeteBecker [[India]] get selected and it substitute as India]] [[abcd 1234 that is the main issue I also tried same thing that you told before asking the question Thanks for your time – psyche Dec 18 '12 at 14:45
  • @BoBTFish can we do something that if after [[ if ]] is encountered before | than that does not match with regex?? – psyche Dec 18 '12 at 14:52
  • 1
    @BoBTFish I thought that `*?` (as opposed to just `*`) would be the opposite of greedy. – James Kanze Dec 18 '12 at 15:18
  • 1
    @PragneshPatel Instead of `.*?` in the capture group, try `[^]|]*?`. – James Kanze Dec 18 '12 at 15:19