473,386 Members | 1,721 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,386 software developers and data experts.

regex: before and after the match

I want to match a table and also capture the parts before and after the
table:
preg_match_all("|(.*)(<table.*table>)(.*)|",$text, $parts);
But of course it doesn't work, presumably because the first .* is
greedy. I tried adding an ? but that doesn't work either. How do I do
this ?
Jul 17 '05 #1
3 3508

"meltedown" <fa**@fake.net> wrote in message
news:Ec***********************@news.easynews.com.. .
I want to match a table and also capture the parts before and after the
table:
preg_match_all("|(.*)(<table.*table>)(.*)|",$text, $parts);
But of course it doesn't work, presumably because the first .* is
greedy. I tried adding an ? but that doesn't work either. How do I do
this ?


You might be confused at the results because they contain the table HTML.
But you were right first time.

$text="|before<table>stuff goes here</table>after|";
print htmlspecialchars($text)."<br>";
preg_match_all("|(.*?)(<table.*table>)(.*)|",$text , $parts);

print("Before: ".$parts[1][0]);
print "<br>Inner: ";
print htmlspecialchars($parts[2][0]);
print "<br>";
print("After: ".$parts[3][0]);

Results:
|before<table>stuff goes here</table>after|
Before: |before
Inner: <table>stuff goes here</table>
After: after|
Isn't this what you wanted?

HTH

Garp


Jul 17 '05 #2
"meltedown" <fa**@fake.net> wrote in message
news:Ec***********************@news.easynews.com.. .
I want to match a table and also capture the parts before and after the
table:
preg_match_all("|(.*)(<table.*table>)(.*)|",$text, $parts);
But of course it doesn't work, presumably because the first .* is
greedy. I tried adding an ? but that doesn't work either. How do I do
this ?


Yes, you need to make the first .* non-greedy by appending a ? (probably the
second .* too). The reason why it doesn't work is because you're not using
the s modifier, without which . doesn't match newline characters. Make sense
to use the i modifier as well for case insensitivity. And preg_match()
should suffice in this case, since you'll at most have one match.
Jul 17 '05 #3
Garp wrote:
"meltedown" <fa**@fake.net> wrote in message
news:Ec***********************@news.easynews.com.. .
I want to match a table and also capture the parts before and after the
table:
preg_match_all("|(.*)(<table.*table>)(.*)|",$text, $parts);
But of course it doesn't work, presumably because the first .* is
greedy. I tried adding an ? but that doesn't work either. How do I do
this ?

You might be confused at the results because they contain the table HTML.
But you were right first time.

OK thanks. I did get it to work with preg_match
I think I was somehow abusing preg_match_all-

Jul 17 '05 #4

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

Similar topics

2
by: Daniel Billingsley | last post by:
First, if MSFT is listening I'll say IMO the MSDN material is sorely lacking in this area... it's just a whole bunch of information thrown at you and you're left to yourself as to organizing it in...
3
by: Jeff McPhail | last post by:
I am using Regex.Match in a large application and the memory is growing out of control. I have tried several ways to try and release the memory and none of them work. Here are some similar examples...
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: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
5
by: Kofi | last post by:
Any takers? Got a string of DNA as an input sequence GGATGGATG, apply the simple regex "GGATG" as in Regex r = new Regex("GGATG", (RegexOptions.Compiled)); MatchCollection matches =...
3
by: spamsickle | last post by:
I have a Perl background, so some of what I know in other contexts is probably getting in the way of what I need to learn now. With that said, I'm having a problem getting my regex to work as I...
3
by: aspineux | last post by:
My goal is to write a parser for these imaginary string from the SMTP protocol, regarding RFC 821 and 1869. I'm a little flexible with the BNF from these RFC :-) Any comment ? tests= def...
1
by: jonnyboy6969 | last post by:
Hi All Really hoping someone can help me out here with my deficient regex skills :) I have a function which takes a string of HTML and replaces a term (word or phrase) with a link. The pupose...
1
by: al.moorthi | last post by:
the below program is working in Suse and not working on Cent 5: can any body have the solution ? #include <regex.h> #include <stdlib.h> #include <stdio.h> int main(){ char cool =...
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
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
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...

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.