Re: find function of String
Thank you for all your responses. I guess I am not very clear in my
question. Actually it is nothing complicated. All text except [ or ]
needs to be extracted. there is no complex stuff of subsets etc...
Anywayz I found a simple way using boost::regex to match only the
characters followed by numbers
int main(int argc, char* argv[])
{
try
{
cout << "[START REGEXP CHECKING]" << endl;
// String to be validated
string myString = "[SBD 65 [INI 25 [PUP 0 ][RPN 25 ]][ANA 1 ][BUIPNR
0 ][BUIPAX 1 ]";
// Regular Expression to be used.
string myRegexp = "[a-zA-Z]+\\s\\d+";
boost::regex aMyRegExp(myRegexp);
// Needed to extract some part from myString, considering the sub-
expressions grouped by "()"
boost::cmatch matches;
cout << endl << "String: " << myString;
cout << endl << "Regular Expression: " << myRegexp << endl;
// Checking if myString matches the Regular Expression.
// Use boost::regex_match(myString.c_str(), aMyRegExp) if the
extraction is not needed
std::string::const_iterator start, end;
start = myString.begin();
end = myString.end();
boost::match_results<std::string::const_iterator> what;
boost::match_flag_type flags = boost::match_default;
while (regex_search(start, end, what, aMyRegExp, flags)){
cout << "Size: " << what.size() << endl;
cout << "Result: " << what << endl;
// update start position
start = what[0].second;
}
cout << endl << endl << "[END REGEXP CHECKING]" << endl;
return 0;
}
catch (...)
{
cout << "EXCEPTION!!!!" << endl;
return 1;
}
}
This prints out only text in the format of XYZ 34 etc
Regards,
Prashant
On Oct 20, 1:38 am, "Jim Langston" <tazmas...@rocketmail.com> wrote:
"coolchap" <b.prash...@gmail.com> wrote in message
news:367c854d-8268-4721-9cb3-c36c73cd1947@i21g2000yqg.googlegroups.com...
Hi,
I am basically looking to find a simple way to parse and
extract text values between braces of a string. The braces can be
either [ or ]. This has to be done using C++.
example
[SBD 65 [INI 25 [PUP 0 ][RPN 25 ]][ANA 1 ][BUIPNR 0 ][BUIPAX 1 ]
[BUIBKG 0 ][BUIOPE 0 ][BUINCM 0 ][BUIREF 0 ][BUIAIR 0 ][BUISBR 0 ]
[BUITSM 0 ][BUIFAR 0 ][BUIRFL 0 ][BUIGRP 0 ][BUIPDI 0 ]
All values like SBD 65, INI 25, PUP 0 etc needs to be extracted.
You need to explain your conditions more. Extracted where and how? Fro=
m
what you have given it seems simple enough to just extract everything tha=
t's
not a [ or a ]. However, from the data given it seems that RPN 25 is
somehow a subset or associated with INI 25. Yet your output does not
express this information. Does it need to? Will it need to in the f=
uture?
Or can a simple extraction of the text work? Will [ or ] ever be part =
of
the text? If so, will it be escaped? What are the allowable charact=
ers for
data (may or may not matter depending on algorithm). Etc...