de*************@yahoo.co.in wrote:
Hello,
I am relatively new to the world of regex and require some help in
forming a regular expression to achieve the following:
I have an input stream similar to:
Slot: slot1
Description: this is a description
Slot: slot2
Description: this is the second description
Attribute1: atrra
Attribute2: attrb
Slot: slot3
Description: this is the third description
Attribute1: atrrc
Attribute2: attrd
I have to pick all the values for "Slot" and corresponding
"Attribute2". If there is no "Attribute2" for a "Slot", I need to skip
that Slot.
I arrived at a regular exp similar to:
"^Slot:\\s+(.*?)\\s+Description:\\s+.*?\\s+Attribu te1:\\s+.*?\\s+Attribute2:\\s+(.*?)\\s+.*?$"
but no luck. For the above example, this gives me "slot1 and attrd",
wheras I was expecting slot2, attrb and slot3, attrd.
Any thoughts on this is highly appreciated.
As written, the regular expression requires the presence of all three of
Definition, Attribute1, and Attribute2 for a match. So with your
example, the Definition portion of the first Slot swallows all the input
up to Attribute1, that is, the definition field includes the beginnning
of the text for the next Slot.
You need to make the attributes optional. Change
"Attribute1:\\s+.*?\\s+"
to
"(?:Attribute1:\\s+.*?)?"
and similarly for attribute2. (Also take out the ^ an $; they're just
confusing the issue) That way you can pick out the first slot
definition. Then check whether it had an attribute 2, and if not, repeat
the search, starting at the first character after the match. In general
that kind of repetitive search is tricky to get right, so you're better
off letting the implementor handle the details. With TR1's regular
expressions, the code would look something like this:
using namespace std::tr1;
regex rgx( regular expression goes here );
const char *text = sample text goes here;
cregex_iterator first(text, text + strlen(text), rgx);
cregex_iterator last;
while (first != last)
{
if ((*first)[1].matched)
cout << *first << '\n';
++first;
}
For more details, see chapter 19 of my book, "The C++ Standard Library
Extensions," which will be in bookstores later this month. It covers all
of TR1.