I have a the following sentence:
[[Image:Levellers declaration and standard.gif|thumb|200px|Woodcut from a [[Diggers]] document by william everard]]
Regular expression
I am using the following regular expression in order to extract the words that are between brackets [[ ]]:
regex = "\\[\\[(.*?)\\]\\]"
The output should be equal to the following sentence:
Image:Levellers declaration and standard.gif|thumb|200px|Woodcut from a [[Diggers]] document by william everard
I want to only take into consideration the left-most brackets ]]
Problem
regex will extract [[Image:Levellers declaration and standard.gif|thumb|200px|Woodcut from a [[Diggers]]
and will leave document by william everard]]
.
Question
How can I ignore the inner brackets represented by ]].
Update V0
I wrote a simple program inspired by BalancedParentheses.cpp in order to locate the beginning and ending of the text between brackets in a string.
Source code
#include <stack>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
bool AreParanthesesBalanced(string exp)
{
stack<char> S;
vector<pair<int, int>> index;
int end;
vector<int> start;
for(int i = 0; i < exp.length(); i++)
{
if(exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
{
S.push(exp[i]);
start.push_back(i);
}
else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']')
{
if(S.empty() || !ArePair(S.top(),exp[i]))
{
return false;
}
else
{
S.pop();
end = i;
index.push_back(make_pair(start[start.size() - 1] ,end));
start.pop_back();
}
}
}
for(int i = 0; i < index.size(); i ++)
{
cout << index[i].first <<" "<< index[i].second << endl;
}
return S.empty() ? true:false;
}
int main()
{
string exp = "[[Image:Levellers declaration and standard.gif|thumb|200px|Woodcut from a [[Diggers]] document by william everard]] ";
bool x = reParanthesesBalanced(exp);
return 0;
}
Output
75 83
74 84
1 113
0 114