473,289 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Need help in forming a regular expression using regex_replace

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.

Thanks in advance!

Regards,
Deepak

Jul 11 '06 #1
6 2207
<de*************@yahoo.co.inwrote:
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/
Jul 11 '06 #2

Robbie Hatley wrote:
<de*************@yahoo.co.inwrote:
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

Jul 11 '06 #3
de*************@yahoo.co.in 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+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.
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

Jul 11 '06 #4
On 11 Jul 2006 04:08:29 -0700 in comp.lang.c++,
de*************@yahoo.co.in wrote,
>I arrived at a regular exp similar to:
"^Slot:\\s+(.*?)\\s+Description:\\s+.*?\\s+Attrib ute1:\\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.

Jul 11 '06 #5
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.
Jul 11 '06 #6

"mlimber" <ml*****@gmail.comwrote:
Robbie Hatley wrote:
<de*************@yahoo.co.inwrote:
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/
Jul 14 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: derek.google | last post by:
I hope a Boost question is not too off-topic here. It seems that upgrading to Boost 1.33 broke some old regex code that used to work. I have reduced the problem to this simple example: cout <<...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
6
by: JohnSouth | last post by:
Hi I've been using a Regular expression to test for valid email addresses. It looks like: \w+(\w+)*@\w+(\w+)*\.\w+(\w+)* I've now had 2 occassions where it has rejected and email address...
3
by: Joe | last post by:
Hi, I have been using a regular expression that I don’t uite understand to filter the valid email address. My regular expression is as follows: <asp:RegularExpressionValidator...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
3
by: Lucky | last post by:
hi guys, i'm practising regular expression. i've got one string and i want it to split in groups. i was trying to make one regular expression but i didn't successed. please help me guys. i'm...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.