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

Any regex pro's out there?

nel
I have two tags:
<!--// Remove Begin //--and <!--// Remove End //-->

I want to use regi_replace() to remove everything between these tags.

The thing is, these tags can be repeated throughout the code.

<!--// Remove Begin //-->(.+)<!--// Remove End //--works, but only
if the tags exists once. Otherwise, it parses out everything between
the first <!--// Remove Begin //--and the last <!--// Remove End //--
>.
How could i modify this so that it will...

convert: aaa<!--// Remove Begin //-->bbb <!--// Remove End //--
>ccc<!--// Remove Begin //-->ddd<!--// Remove End //-->
into: aaaccc
??

Jun 5 '07 #1
5 2147
On Jun 5, 2:18 am, nel <NajibKa...@gmail.comwrote:
I have two tags:
<!--// Remove Begin //--and <!--// Remove End //-->

I want to use regi_replace() to remove everything between these tags.

The thing is, these tags can be repeated throughout the code.

<!--// Remove Begin //-->(.+)<!--// Remove End //--works, but only
if the tags exists once. Otherwise, it parses out everything between
the first <!--// Remove Begin //--and the last <!--// Remove End //--
.

How could i modify this so that it will...

convert: aaa<!--// Remove Begin //-->bbb <!--// Remove End //-->ccc<!--// Remove Begin //-->ddd<!--// Remove End //-->

into: aaaccc

??
you need to add a rule: "remove .+ but not if .+ contains the end
marker <!--// Remove End //-->
I am assuming you are doing this inside a [webpage?] where < or
possibly <!-- will be present WITHIN the sections to be removed, so
bbb
could be
<!--comment--><b>hello</>bb
If html will not be present you could simply use a NOT instruction to
look for <
[^<]+

Jun 5 '07 #2
On Jun 5, 2:18 am, nel <NajibKa...@gmail.comwrote:
I have two tags:
<!--// Remove Begin //--and <!--// Remove End //-->

I want to use regi_replace() to remove everything between these tags.

The thing is, these tags can be repeated throughout the code.

<!--// Remove Begin //-->(.+)<!--// Remove End //--works, but only
if the tags exists once. Otherwise, it parses out everything between
the first <!--// Remove Begin //--and the last <!--// Remove End //--
.

How could i modify this so that it will...

convert: aaa<!--// Remove Begin //-->bbb <!--// Remove End //-->ccc<!--// Remove Begin //-->ddd<!--// Remove End //-->

into: aaaccc

??
i should have added, google for ungreedy U switch - your matching is
too greedy, and slurps up one giant match rather than many "least"
matches

Jun 5 '07 #3
Rik
On Tue, 05 Jun 2007 03:56:05 +0200, shimmyshack <ma********@gmail.com>
wrote:
On Jun 5, 2:18 am, nel <NajibKa...@gmail.comwrote:
>I have two tags:
<!--// Remove Begin //--and <!--// Remove End //-->

I want to use regi_replace() to remove everything between these tags.

The thing is, these tags can be repeated throughout the code.

<!--// Remove Begin //-->(.+)<!--// Remove End //--works, but only
if the tags exists once. Otherwise, it parses out everything between
the first <!--// Remove Begin //--and the last <!--// Remove End //--

How could i modify this so that it will...

i should have added, google for ungreedy U switch - your matching is
too greedy, and slurps up one giant match rather than many "least"
matches
Or just use the ? modifier:
preg_replace('|<!--// Remove Begin //-->.*?<!--// Remove End
//-->|si','',$string);

--
Rik Wasmus
Jun 5 '07 #4
On Jun 5, 11:20 am, Rik <luiheidsgoe...@hotmail.comwrote:
On Tue, 05 Jun 2007 03:56:05 +0200, shimmyshack <matt.fa...@gmail.com>
wrote:
On Jun 5, 2:18 am, nel <NajibKa...@gmail.comwrote:
I have two tags:
<!--// Remove Begin //--and <!--// Remove End //-->
I want to use regi_replace() to remove everything between these tags.
The thing is, these tags can be repeated throughout the code.
<!--// Remove Begin //-->(.+)<!--// Remove End //--works, but only
if the tags exists once. Otherwise, it parses out everything between
the first <!--// Remove Begin //--and the last <!--// Remove End //--
How could i modify this so that it will...
i should have added, google for ungreedy U switch - your matching is
too greedy, and slurps up one giant match rather than many "least"
matches
Or just use the ? modifier:
preg_replace('|<!--// Remove Begin //-->.*?<!--// Remove End
//-->|si','',$string);
--
Rik Wasmus
Just a side note to nel, if you are going to use shimmyshack's U
modifier you have to use PCRE instead as Rik is doing, and be sure not
to copy Rik's exact pattern unless you switch because you are using
PHP's built in regex functions.

At least, I think you are using PHP's built-in regex stuff, assuming
that by regi_replace() you mean eregi_replace()

-Mike PII

Jun 6 '07 #5
nel
Yep thanks, I realized that when I googled "U modifier".

This is what I'm using in case anyone wants to know:

//first replaces any line breaks with a token
identifier since preg_replace doesn't work with multiply lines
$cleaned_content = str_ireplace("\n","<!--// New Line //-->",
$content);
//this creates our regular query sequence... perl-
stylezzz
$reg = '/<!--\/\/ Remove Begin \/\/-->(.+)<!--\/\/ Remove End \/\/--
>/U';
$cleaned_content = preg_replace($reg,"",$cleaned_content);
//now just put our line breaks back into place
$cleaned_content = str_ireplace("<!--// New Line //-->","\n",
$cleaned_content);

The above code will replace everything in my string (which I pulled
from an HTML file) with all the <!--// Remove Begin //--tags and
<!--// Remove End //--and anything in between them removed!

The U modifier solved my problem where It was making "abxxab" into ""
when it's supposed to replace everything between a and b instead of
making "abxxab" into "xx";

Thanks again!
-nel
On Jun 5, 8:04 pm, Mike P2 <sumguyovrt...@gmail.comwrote:
On Jun 5, 11:20 am, Rik <luiheidsgoe...@hotmail.comwrote:
On Tue, 05 Jun 2007 03:56:05 +0200, shimmyshack <matt.fa...@gmail.com>
wrote:
On Jun 5, 2:18 am, nel <NajibKa...@gmail.comwrote:
>I have two tags:
><!--// Remove Begin //--and <!--// Remove End //-->
>I want to use regi_replace() to remove everything between these tags.
>The thing is, these tags can be repeated throughout the code.
><!--// Remove Begin //-->(.+)<!--// Remove End //--works, but only
>if the tags exists once. Otherwise, it parses out everything between
>the first <!--// Remove Begin //--and the last <!--// Remove End //--
>How could i modify this so that it will...
i should have added, google for ungreedy U switch - your matching is
too greedy, and slurps up one giant match rather than many "least"
matches
Or just use the ? modifier:
preg_replace('|<!--// Remove Begin //-->.*?<!--// Remove End
//-->|si','',$string);
--
Rik Wasmus

Just a side note to nel, if you are going to use shimmyshack's U
modifier you have to use PCRE instead as Rik is doing, and be sure not
to copy Rik's exact pattern unless you switch because you are using
PHP's built in regex functions.

At least, I think you are using PHP's built-in regex stuff, assuming
that by regi_replace() you mean eregi_replace()

-Mike PII

Jun 6 '07 #6

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

Similar topics

2
by: mikea59 | last post by:
I am getting errors in XMLSpy (Pro) in the following case: Source Document: <test> 12345 AB 12345 </test> Stylesheet: <xsl:stylesheet version="2.0"...
16
by: Stephane | last post by:
Hi, I'm trying to replace parenthesis using Regex.replace but I'm always having this error: System.ArgumentException: parsing ":-)" - Too many )'s. Parameter name: :-) Here's my code: ...
0
by: Hugo Wetterberg | last post by:
Hi, I've got a problem with RegEx in the 2.0 Beta. I have a regexp expression that works fine in the 1.1 version of the framework but breaks in 2.0. I'm no regexp guru, so I don't really know why...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
17
by: steve | last post by:
here's the deal...cvs, tick encapsulted data. trying to use regex's to validate records. here's an example row: 'AD,'BF','132465','06/09/2004','','BNSF','A','TYPE','1278','','BR','2999',''...
2
by: G | last post by:
Hello, I have a REGEX validator which checks for DIGITS ONLY, 10 or 11 digits in length. The pattern is:# ^\d{10,11}$ I would like to enhance this pattern so in addition to only digits,...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
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....
4
by: seberino | last post by:
I'm looking over the docs for the re module and can't find how to "NOT" an entire regex. For example..... How make regex that means "contains regex#1 but NOT regex#2" ? Chris
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
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
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...

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.