find function of String

C

coolchap

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.

At present I am trying to use the 'find' function of string and 'sub-
string' method to achieve this.

If there is a simpler solution, please do let me know your ideas.

Regards,
Prashant
 
J

Jorgen Grahn

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.

Why isn't "SBD 65 [INI 25 [PUP 0 ][RPN 25 ]" a value in your example?
And what would the input "[foo ]]" mean?
I get a feeling you haven't described the input language completely.

/Jorgen
 
A

Alf P. Steinbach /Usenet

* coolchap, on 19.10.2010 17:03:
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.

At present I am trying to use the 'find' function of string and 'sub-
string' method to achieve this.

If there is a simpler solution, please do let me know your ideas.

Much depends on the detailed language spec and whether you need to check the
input for syntactical correctness.

With the most forgiving requirements I'd probably just code a loop scanning
through the string character by character, like a state machine.

A more general approach is shown below. I coded that up just for fun, and
because it's difficult to describe this approach but easy to just show it:


<code>
#include <iostream>
#include <sstream> // istringstream
#include <string>
#include <stdlib.h> // EXIT_SUCCESS, EXIT_FAILURE
#include <stdexcept>
#include <ctype.h> // isspace, isupper, isdigit
#include <stdio.h> // EOF
using namespace std;

bool throwX( string const& s ) { throw runtime_error( s ); }

typedef unsigned char UChar;

class UCharCode
{
private:
int code_;

public:
UCharCode(): code_( 0 ) {}

UCharCode( int code )
: code_( code == EOF? EOF : UChar( code ) )
{}

int value() const { return code_; }
};

bool isSpace( UCharCode code )
{
return isspace( code.value() );
}

bool isUppercase( UCharCode code )
{
return isupper( code.value() );
}

bool isDigit( UCharCode code )
{
return isdigit( code.value() );
}

class Token
{
public:
struct Kind
{
enum Enum
{
notAToken = 0,
leftBracket, rightBracket, identifier, number,
end
};
};

private:
string spec_;
Kind::Enum kind_;

void makeIdentifierFrom( istream& stream )
{
kind_ = Kind::identifier;
do
{
spec_ += char( stream.get() );
} while( isUppercase( stream.peek() ) );
}

void makeNumberFrom( istream& stream )
{
kind_ = Kind::number;
do
{
spec_ += char( stream.get() );
} while( isDigit( stream.peek() ) );
}

public:
Token(): kind_( Kind::notAToken ) {}

explicit Token( istream& stream )
{
int const charCode = stream.peek();

if( charCode == '[' )
{
kind_ = Kind::leftBracket; spec_ = char( stream.get() );
}
else if( charCode == ']' )
{
kind_ = Kind::rightBracket; spec_ = char( stream.get() );
}
else if( isUppercase( charCode ) )
{
makeIdentifierFrom( stream );
}
else if( isDigit( charCode ) )
{
makeNumberFrom( stream );
}
else if( charCode == EOF )
{
kind_ = Kind::end;
}
else
{
kind_ = Kind::notAToken; spec_ = char( stream.get() );
}
}

string const& spec() const { return spec_; }
Kind::Enum kind() const { return kind_; }
};

class Lexer
{
private:
istream& input_;
Token currentToken_;

public:
explicit Lexer( istream& aStream ): input_( aStream ) { advance(); }

void skipSpaces()
{
while( isSpace( input_.peek() ) )
{
input_.get();
}
}

void advance()
{
skipSpaces();
currentToken_ = Token( input_ );
}

Token const& current() const { return currentToken_; }

Token next()
{
advance();
return current();
}
};

void analyze( string const& spec )
{
istringstream input( spec );
Lexer lexer( input );

while( lexer.current().kind() != Token::Kind::end )
{
(lexer.current().kind() != Token::Kind::notAToken)
|| throwX( "analyze: invalid token \"" + lexer.current().spec() +
"\"" );

if( lexer.current().kind() == Token::Kind::identifier )
{
Token const id = lexer.current();

lexer.advance();
(lexer.current().kind() == Token::Kind::number)
|| throwX( "analyze: identifier not followed by number" );
cout << id.spec() << " " << lexer.current().spec() << endl;
}
lexer.advance();
}
}

void cppMain()
{
string const data =
"[SBD 65 [INI 25 s [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 ]";

analyze( data );
}

int main()
{
try
{
cppMain();
return EXIT_SUCCESS;
}
catch( exception const& x )
{
cerr << "!" << x.what() << endl;
}
return EXIT_FAILURE;
}
</code>


Cheers & hth.,

- Alf
 
J

Jim Langston

coolchap said:
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? From
what you have given it seems simple enough to just extract everything that'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 future?
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 characters for
data (may or may not matter depending on algorithm). Etc...
 
C

coolchap

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

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? From
what you have given it seems simple enough to just extract everything that'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 future?
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 characters for
data (may or may not matter depending on algorithm).  Etc...
 
A

Alf P. Steinbach /Usenet

* coolchap, on 20.10.2010 17:56:
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...

In that case, how about

<code>
#include <algorithm>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

void analyze( string s )
{
replace( s.begin(), s.end(), '[', ' ' );
replace( s.begin(), s.end(), ']', ' ' );

istringstream stream( s );
string id, numSpec;

while( stream >> id >> numSpec )
{
cout << id << ' ' << numSpec << endl;
}
}

int main()
{
analyze( "[SBD 65 [INI 25 [PUP 0 ][RPN 25 ]][ANA 1 ][BUIPNR0 ][BUIPAX 1 ]" );
}
</code>

?

Anywayz I found a simple way using boost::regex to match only the
characters followed by numbers


Some people, when confronted with a problem, think “I know, I'll use regular
expressions.” Now they have two problems.
- Jamie Zawinski

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


He he. :)


Cheers & hth.,

- Alf
 
A

Alf P. Steinbach /Usenet

* coolchap, on 20.10.2010 17:56:
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...

In that case, how about

<code>
#include <algorithm>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

void analyze( string s )
{
replace( s.begin(), s.end(), '[', ' ' );
replace( s.begin(), s.end(), ']', ' ' );

istringstream stream( s );
string id, numSpec;

while( stream >> id >> numSpec )
{
cout << id << ' ' << numSpec << endl;
}
}

int main()
{
analyze( "[SBD 65 [INI 25 [PUP 0 ][RPN 25 ]][ANA 1 ][BUIPNR0 ][BUIPAX 1 ]" );
}
</code>

?

Anywayz I found a simple way using boost::regex to match only the
characters followed by numbers


Some people, when confronted with a problem, think “I know, I'll use regular
expressions.” Now they have two problems.
- Jamie Zawinski

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


He he. :)


Cheers & hth.,

- Alf
 
A

Alf P. Steinbach /Usenet

* coolchap, on 20.10.2010 17:56:
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...

In that case, how about

<code>
#include <algorithm>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

void analyze( string s )
{
replace( s.begin(), s.end(), '[', ' ' );
replace( s.begin(), s.end(), ']', ' ' );

istringstream stream( s );
string id, numSpec;

while( stream >> id >> numSpec )
{
cout << id << ' ' << numSpec << endl;
}
}

int main()
{
analyze( "[SBD 65 [INI 25 [PUP 0 ][RPN 25 ]][ANA 1 ][BUIPNR0 ][BUIPAX 1 ]" );
}
</code>

?
Anywayz I found a simple way using boost::regex to match only the
characters followed by numbers


Some people, when confronted with a problem, think “I know, I'll use regular
expressions.” Now they have two problems.
- Jamie Zawinski


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

He he... :)


Cheers & hth.,

- Alf
 
A

Alf P. Steinbach /Usenet

* coolchap, on 20.10.2010 17:56:
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...

In that case, how about

<code>
#include <algorithm>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

void analyze( string s )
{
replace( s.begin(), s.end(), '[', ' ' );
replace( s.begin(), s.end(), ']', ' ' );

istringstream stream( s );
string id, numSpec;

while( stream >> id >> numSpec )
{
cout << id << ' ' << numSpec << endl;
}
}

int main()
{
analyze( "[SBD 65 [INI 25 [PUP 0 ][RPN 25 ]][ANA 1 ][BUIPNR0 ][BUIPAX 1 ]" );
}
</code>

?

Anywayz I found a simple way using boost::regex to match only the
characters followed by numbers


Some people, when confronted with a problem, think “I know, I'll use regular
expressions.” Now they have two problems.
- Jamie Zawinski

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

He he... :)


Cheers & hth.,

- Alf
 
A

Alf P. Steinbach

* coolchap, on 20.10.2010 17:56:
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...

In that case, how about

<code>
#include <algorithm>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

void analyze( string s )
{
replace( s.begin(), s.end(), '[', ' ' );
replace( s.begin(), s.end(), ']', ' ' );

istringstream stream( s );
string id, numSpec;

while( stream >> id >> numSpec )
{
cout << id << ' ' << numSpec << endl;
}
}

int main()
{
analyze( "[SBD 65 [INI 25 [PUP 0 ][RPN 25 ]][ANA 1 ][BUIPNR0 ][BUIPAX 1 ]" );
}
</code>

?

Anywayz I found a simple way using boost::regex to match only the
characters followed by numbers


Some people, when confronted with a problem, think “I know, I'll use regular
expressions.” Now they have two problems.
- Jamie Zawinski

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

He he... :)


Cheers & hth.,

- Alf
 
A

Alf P. Steinbach

* coolchap, on 20.10.2010 17:56:
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...

In that case, how about

<code>
#include <algorithm>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

void analyze( string s )
{
replace( s.begin(), s.end(), '[', ' ' );
replace( s.begin(), s.end(), ']', ' ' );

istringstream stream( s );
string id, numSpec;

while( stream >> id >> numSpec )
{
cout << id << ' ' << numSpec << endl;
}
}

int main()
{
analyze( "[SBD 65 [INI 25 [PUP 0 ][RPN 25 ]][ANA 1 ][BUIPNR0 ][BUIPAX 1 ]" );
}
</code>

?

Anywayz I found a simple way using boost::regex to match only the
characters followed by numbers


Some people, when confronted with a problem, think “I know, I'll use regular
expressions.” Now they have two problems.
- Jamie Zawinski

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

He he... :)


Cheers & hth.,

- Alf
 
A

Alf P. Steinbach

* coolchap, on 20.10.2010 17:56:
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...

In that case, how about

<code>
#include <algorithm>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

void analyze( string s )
{
replace( s.begin(), s.end(), '[', ' ' );
replace( s.begin(), s.end(), ']', ' ' );

istringstream stream( s );
string id, numSpec;

while( stream >> id >> numSpec )
{
cout << id << ' ' << numSpec << endl;
}
}

int main()
{
analyze( "[SBD 65 [INI 25 [PUP 0 ][RPN 25 ]][ANA 1 ][BUIPNR0 ][BUIPAX 1 ]" );
}
</code>

?

Anywayz I found a simple way using boost::regex to match only the
characters followed by numbers


Some people, when confronted with a problem, think “I know, I'll use regular
expressions.” Now they have two problems.
- Jamie Zawinski

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

He he... :)


Cheers & hth.,

- Alf
 
A

Alf P. Steinbach /Usenet

* coolchap, on 20.10.2010 17:56:

I apologize for multiple identical replies. Thunderbird at first reported
unspecified errors while posting, and then even when the postings succeeded they
didn't show up. And they still don't show, on the EternalSeptember server, but I
see them on AIOE (I had to double-check, it was just too inconsistent when TB
reported that /connecting/ to EternalSeptember failed, but didn't have any
trouble retrieving articles in other groups).

Argh. One super-buggy wooly system using another buggy super-wooly system, and
so on. No firm ground.


Cheers,

- Alf
 
A

Alf P. Steinbach

* coolchap, on 20.10.2010 17:56:

I apologize for multiple identical replies. Thunderbird at first reported
unspecified errors while posting, and then even when the postings succeeded they
didn't show up. And they still don't show, on the EternalSeptember server, but I
see them on AIOE (I had to double-check, it was just too inconsistent when TB
reported that /connecting/ to EternalSeptember failed, but didn't have any
trouble retrieving articles in other groups).

Argh. One super-buggy wooly system using another buggy super-wooly system, and
so on. No firm ground.


Cheers,

- Alf
 
J

Jim Langston

Alf P. Steinbach said:
* coolchap, on 20.10.2010 17:56:

I apologize for multiple identical replies. Thunderbird at first reported
unspecified errors while posting, and then even when the postings
succeeded they didn't show up. And they still don't show, on the
EternalSeptember server, but I see them on AIOE (I had to double-check, it
was just too inconsistent when TB reported that /connecting/ to
EternalSeptember failed, but didn't have any trouble retrieving articles
in other groups).

Argh. One super-buggy wooly system using another buggy super-wooly system,
and so on. No firm ground.


Cheers,

- Alf

I don't know weather to laugh or cry that you multiple posted an apology for
multiple posting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top