473,791 Members | 2,985 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help extracting tag with boost:regex

MCH
hi there,
I am working with a HTML-like text with boost:regex. For example,
the following pattern might occur in my text

<abc efg>
[TAG] <p>EFG</p[/TAG] 12<3>

In this case, I would like to extract everything between [TAG] [/TAG]
and replace [TAG] with <pre>, [/TAG] with </pre>. Meanwhile,
everything outside [TAG][/TAG] should be unchaged except that < is
replaced by &lt; and is replaced by &gt;

In far more complicated case, a nested [TAG] might occur as follow

<abc efg>
[TAG] <p>EF [TAG] eee [/TAG] G</p[/TAG] 12<3>

in this case, the program just tackle the outermost TAG and left the
inside TAG there. I try to implement the program with boost::regex;
however, it seems never succeed in extracing the TAG even for the
simple case.

Mar 12 '07 #1
3 2811
On 12 Mar 2007 13:07:26 -0700 in comp.lang.c++, "MCH" <gm****@21cn.co m>
wrote,
><abc efg>
[TAG] <p>EFG</p[/TAG] 12<3>

In this case, I would like to extract everything between [TAG] [/TAG]
and replace [TAG] with <pre>, [/TAG] with </pre>. Meanwhile,
everything outside [TAG][/TAG] should be unchaged except that < is
replaced by &lt; and is replaced by &gt;
This exceeds what I know how to do with regex. Too much context.
I would do it with std::string, std::find_first _of(),
and a couple of if statements.

Maybe you could pretend you are writing a Perl program and get some help
from the real regex experts over on comp.lang.perl. misc

Mar 13 '07 #2
MCH wrote:
hi there,
I am working with a HTML-like text with boost:regex. For example,
the following pattern might occur in my text

<abc efg>
[TAG] <p>EFG</p[/TAG] 12<3>

In this case, I would like to extract everything between [TAG] [/TAG]
and replace [TAG] with <pre>, [/TAG] with </pre>. Meanwhile,
everything outside [TAG][/TAG] should be unchaged except that < is
replaced by &lt; and is replaced by &gt;

In far more complicated case, a nested [TAG] might occur as follow

<abc efg>
[TAG] <p>EF [TAG] eee [/TAG] G</p[/TAG] 12<3>

in this case, the program just tackle the outermost TAG and left the
inside TAG there. I try to implement the program with boost::regex;
however, it seems never succeed in extracing the TAG even for the
simple case.
try
const boost::regex expression("\[TAG\].*?\[\/TAG\]");

The trick is to use the '?' after .* to turn off greedy pattern match,
instead of matching the last occurance of [/TAG], it will match the
first [/TAG] which may or may not be what you want. It seems your
problem is not so much as boost::regex but utilizing regular expression
pattern match in general. I would recommend you to consult regular
expression documents first and experiment with simpler string pattern
with boost::regex.

Fei
Mar 14 '07 #3
Fei Liu wrote:
MCH wrote:
>hi there,
I am working with a HTML-like text with boost:regex. For example,
the following pattern might occur in my text

<abc efg>
[TAG] <p>EFG</p[/TAG] 12<3>

In this case, I would like to extract everything between [TAG] [/TAG]
and replace [TAG] with <pre>, [/TAG] with </pre>. Meanwhile,
everything outside [TAG][/TAG] should be unchaged except that < is
replaced by &lt; and is replaced by &gt;

In far more complicated case, a nested [TAG] might occur as follow

<abc efg>
[TAG] <p>EF [TAG] eee [/TAG] G</p[/TAG] 12<3>

in this case, the program just tackle the outermost TAG and left the
inside TAG there. I try to implement the program with boost::regex;
however, it seems never succeed in extracing the TAG even for the
simple case.
try
const boost::regex expression("\[TAG\].*?\[\/TAG\]");

The trick is to use the '?' after .* to turn off greedy pattern match,
instead of matching the last occurance of [/TAG], it will match the
first [/TAG] which may or may not be what you want.
It's definitely not what's needed, since it will match the [TAG] of
the outer block to the [/TAG] of the inner block. See the "far more
complicated case" above.
It seems your
problem is not so much as boost::regex but utilizing regular expression
pattern match in general. I would recommend you to consult regular
expression documents first and experiment with simpler string pattern
with boost::regex.
Right. Regular expressions don't deal well with recursive patterns.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Mar 20 '07 #4

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

Similar topics

2
3710
by: Richard Latter | last post by:
Hello All, I am a newbie to the Boost library and I have a question about a simple function. All I would like to do is to create a simple function that can test strings using regular expressions. I've written this function which appears to work on the first pass and then fails on the second with the following message. Unhandle exeption in Credit Card Example.exe (KERNEL32.DLL) 0x(Some address): Microsoft C++ Exception.
7
4837
by: Aek | last post by:
Hi everyone, I am trying to construct a regular expression and format string to use with a boost::regex_replace() In my file the sample text is: // .fx shader file FLOAT JOE 3545f; FLOAT BLOGS -3343f; FLOAT AMBIENT 2300.0f;
1
2153
by: Alan Patterson | last post by:
I have installed boost 1.33.1 and everything compiled and installed fine on Windows using Visual C++ 8.0. I wrote the following simple code to test the regex library and it doesn't seem to work. In the following "Matched" is not output to cout. Am I missing something or do I have a problem with my regex install? #include <string>
1
2861
by: Barry | last post by:
I used Boost.Xpressive after 1.3.4, and found it interesting and helpful, but I suffer from long complie-time. So I wonder there is a way to write the regular expression in Xpressive and use it in Boost.RegEx, since there grammer is simmilar to use
3
2618
by: neino | last post by:
Hello, did anyone of You have problems using boost::regex ? That code below : #include "boost/regex.hpp" /*1*/ int main() /*2*/ { /*3*/ boost::regex reg("*"); /*4*/
13
4536
by: brad | last post by:
Still learning C++. I'm writing some regex using boost. It works great. Only thing is... this code seems slow to me compared to equivelent Perl and Python. I'm sure I'm doing something incorrect. Any tips? #include <boost/regex.hpp> #include <iostream> // g++ numbers.cpp -o numbers -I/usr/local/include/boost-1_35 /usr/local/lib/libboost_regex-gcc41-mt-s.a // g++ numbers.cpp -o numbers.exe
0
2406
by: mark | last post by:
This is something I came across today that might be of interest to others: including boost/regex.hpp causes the functions in regex.h to have corrupt pmatch subexpression match values. I'm not sure why. -mark
2
3003
by: skip | last post by:
A colleague wrote a C++ library here at work which uses the Boost.regex library. I quickly discovered an apparent problem with how it searches. Unlike re.match the regex_match function in that library effectively anchors the match at both the start and the end. Can other people confirm this? Thx, Skip Montanaro
3
4541
by: rottmanj | last post by:
In my application I get a string from my datasource that I need to replace some of the bad chars in the strings. Right now I have written a pretty standard regex that should replace any of the bad chars in the strings. However, instead of removing the bad chars it removes all chars from the string. The bad chars I am trying to replace are /'() Here is the current code that I am testing with. boost::regex const...
0
10427
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10155
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9995
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.