473,406 Members | 2,745 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,406 software developers and data experts.

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 2771
On 12 Mar 2007 13:07:26 -0700 in comp.lang.c++, "MCH" <gm****@21cn.com>
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
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...
7
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...
1
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....
1
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...
3
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*/...
13
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....
0
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...
2
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...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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,...

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.