Need help in forming a regular expression using regex_replace

Discussion in 'C++' started by deepak_kamath_n@yahoo.co.in, Jul 11, 2006.

  1. Guest

    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+Attribute1:\\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.

    Thanks in advance!

    Regards,
    Deepak
    , Jul 11, 2006
    #1
    1. Advertising

  2. <> wrote:

    > I am relatively new to the world of regex and require some help in
    > forming a regular expression...


    This has absolutely nothing to do with C++. Ask this in a group
    that deals more with regexes, like a Gnu utilities group, or
    even a perl group:

    gnu.utils.help
    comp.lang.perl.misc

    Also be aware that many programs that use regexes use non-standard
    extensions. What program are you using your regexes with? Ask in
    a group suitable for that program. Or call it's vendor on the phone.
    Or visit the program's web site. Or read a book or manual on the
    program. Or google for info on the program.

    --
    Cheers,
    Robbie Hatley
    East Tustin, CA, USA
    lone wolf intj at pac bell dot net
    (put "[usenet]" in subject to bypass spam filter)
    http://home.pacbell.net/earnur/
    Robbie Hatley, Jul 11, 2006
    #2
    1. Advertising

  3. mlimber Guest

    Robbie Hatley wrote:
    > <> wrote:
    >
    > > I am relatively new to the world of regex and require some help in
    > > forming a regular expression...

    >
    > This has absolutely nothing to do with C++. Ask this in a group
    > that deals more with regexes, like a Gnu utilities group, or
    > even a perl group:
    >
    > gnu.utils.help
    > comp.lang.perl.misc
    >
    > Also be aware that many programs that use regexes use non-standard
    > extensions. What program are you using your regexes with? Ask in
    > a group suitable for that program. Or call it's vendor on the phone.
    > Or visit the program's web site. Or read a book or manual on the
    > program. Or google for info on the program.


    Not so! This group's stated subject matter is that which is related to
    "the C++ language definition as determined by the ISO/ANSI C++ Standard
    document, and by planned extensions and adjustments." In fact, TR1 (a
    planned extension to the standard library) has a regex component.

    Cheers! --M
    mlimber, Jul 11, 2006
    #3
  4. mlimber Guest

    wrote:
    > 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+Attribute1:\\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.


    Can you show us a minimal but complete sample that demonstrates this
    behavior? Your regular expression might be fine but your use of the
    library might be in error.

    Cheers! --M
    mlimber, Jul 11, 2006
    #4
  5. David Harmon Guest

    On 11 Jul 2006 04:08:29 -0700 in comp.lang.c++,
    wrote,
    >I arrived at a regular exp similar to:
    >"^Slot:\\s+(.*?)\\s+Description:\\s+.*?\\s+Attribute1:\\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.


    My thoughts: your regex is too complex and troublesome for me.

    Read your input line-by-line into a std::string with std::getline.
    If you like regex, use it to parse the lines,
    "([A-Za-z0-9]):(.*)" or suchlike.

    When you have parts you can handle them sensibly, errors are not all
    the same "regex failed" message to the poor user, etc.
    David Harmon, Jul 11, 2006
    #5
  6. Pete Becker Guest

    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+Attribute1:\\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.
    Pete Becker, Jul 11, 2006
    #6
  7. "mlimber" <> wrote:

    > Robbie Hatley wrote:
    > > <> wrote:
    > >
    > > > I am relatively new to the world of regex and require some help in
    > > > forming a regular expression...

    > >
    > > This has absolutely nothing to do with C++. Ask this in a group
    > > that deals more with regexes, like a Gnu utilities group, or
    > > even a perl group:
    > >
    > > gnu.utils.help
    > > comp.lang.perl.misc
    > >
    > > Also be aware that many programs that use regexes use non-standard
    > > extensions. What program are you using your regexes with? Ask in
    > > a group suitable for that program. Or call it's vendor on the phone.
    > > Or visit the program's web site. Or read a book or manual on the
    > > program. Or google for info on the program.

    >
    > Not so! ... TR1 (a planned extension to the standard library)
    > has a regex component.


    If so, awesome, bitchin', and cool. I had to write my own regex
    routines (partly based a couple of non-standard, buggy regex
    functions that come with my compiler) in order to do regexes in
    my programs. It was a lot of hard work, doesn't always work right,
    and is very unportable. If regex gets in the std. lib., that would
    be awesome.

    So perhaps I erred in my earlier comments. If so, my apologies
    to you and the OP.


    --
    Cheers,
    Robbie Hatley
    East Tustin, CA, USA
    lone wolf intj at pac bell dot net
    (put "[usenet]" in subject to bypass spam filter)
    http://home.pacbell.net/earnur/
    Robbie Hatley, Jul 14, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Henry Rawas [MSFT]
    Replies:
    0
    Views:
    1,064
    Henry Rawas [MSFT]
    Sep 24, 2004
  2. VSK
    Replies:
    2
    Views:
    2,266
  3. Replies:
    7
    Views:
    1,816
  4. Yahooooooooo

    boost::regex_replace compiler error

    Yahooooooooo, Jan 22, 2007, in forum: C++
    Replies:
    3
    Views:
    448
  5. Yahooooooooo

    boost::regex_replace issue

    Yahooooooooo, Jan 30, 2007, in forum: C++
    Replies:
    1
    Views:
    1,311
    David Harmon
    Jan 31, 2007
Loading...

Share This Page