473,800 Members | 2,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regex help

Hello everyone,

I am puzzled at PHP's handling of regex. Here's the code:

<?php

$str="aabcc";
$pattern="/((a+)b?(c+))/";

preg_match_all( $pattern,$str,$ matches);
print_r($matche s[0]);

?>

The behaviour I expect from the above would be to match:
a
aa
c
cc
abc
aabc
abcc
aabbcc

The output is ALWAYS the maximum strings :

Array
(
[0] =Array
(
[0] =aabcc
)

[1] =Array
(
[0] =aabcc
)

[2] =Array
(
[0] =aa
)

[3] =Array
(
[0] =cc
)

)
Any idea why the substrings are not picked up?

Thanks
Patrick
Dec 17 '07 #1
10 2107
..oO(Patrick Drouin)
>I am puzzled at PHP's handling of regex. Here's the code:

<?php

$str="aabcc" ;
$pattern="/((a+)b?(c+))/";

preg_match_all ($pattern,$str, $matches);
print_r($match es[0]);

?>

The behaviour I expect from the above would be to match:
a
aa
c
cc
abc
aabc
abcc
aabbcc
Nope. What's returned is the entire matched string and all parenthesized
sub strings (if there are any), but not every single matching point from
during the execution.
>The output is ALWAYS the maximum strings :
Correct.
>Array
(
[0] =Array
(
[0] =aabcc
)

[1] =Array
(
[0] =aabcc
)

[2] =Array
(
[0] =aa
)

[3] =Array
(
[0] =cc
)

)
Any idea why the substrings are not picked up?
The above is exactly what you told preg_match() to return:

0: the entire matched string
1: the first sub pattern: ((a+)b?(c+)) =the entire string again
2: the second sub pattern: (a+) =aa
3: the third sub pattern: (c+) =cc

Micha
Dec 17 '07 #2
Hello Michael,
Nope. What's returned is the entire matched string and all parenthesized
sub strings (if there are any), but not every single matching point from
during the execution.

The above is exactly what you told preg_match() to return:
Well OK, let me rephrase then, how can I tell PHP to match the
substrings. In my mind, (a+) means a, aa, aaa, ... and not only the
maximum string. I don't see how that behaviour is logical in any way.

Thanks,
Patrick
Dec 17 '07 #3
Patrick Drouin wrote:
$pattern="/((a+)b?(c+))/";
$pattern="/(a+)b?(c+)/";

HTH

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 10 days, 21:10.]

Sharing Music with Apple iTunes
http://tobyinkster.co.uk/blog/2007/1...tunes-sharing/
Dec 18 '07 #4
On Mon, 17 Dec 2007 17:03:19 +0100, Patrick Drouin <no**@none.comw rote:
Hello everyone,

I am puzzled at PHP's handling of regex. Here's the code:

<?php

$str="aabcc";
$pattern="/((a+)b?(c+))/";

preg_match_all( $pattern,$str,$ matches);
print_r($matche s[0]);

?>

The behaviour I expect from the above would be to match:
a
aa
c
cc
abc
aabc
abcc
aabbcc
Is this one string or a set of strings you're trying to match? Only the
5th, 6th and 7th line will match this pattern...
>
The output is ALWAYS the maximum strings :
What do you mean by 'maximum'? I see nothing weird here...
--
Rik Wasmus
Dec 18 '07 #5
Moi
Hello,

On 18 déc, 05:34, Toby A Inkster <usenet200...@t obyinkster.co.u k>
wrote:
Patrick Drouin wrote:
$pattern="/((a+)b?(c+))/";

$pattern="/(a+)b?(c+)/";
If you try this, you will see that it spit out

[0] =aaabccc
[0] =aaa
[0] =ccc

That's not what I'm looking for...

Thanks,
P
Dec 18 '07 #6
Moi
Hello Rick,

On 18 déc, 06:52, "Rik Wasmus" <luiheidsgoe... @hotmail.comwro te:
<?php
$str="aabcc";
$pattern="/((a+)b?(c+))/";
preg_match_all( $pattern,$str,$ matches);
print_r($matche s[0]);
?>
The behaviour I expect from the above would be to match:
a
aa
c
cc
abc
aabc
abcc
aabbcc
The output is ALWAYS the maximum strings :

What do you mean by 'maximum'? I see nothing weird here...
OK,what I don't understand is why "a" is not captured as (a+) means
"a" or repeated "a"...

Thanks,
P
Dec 18 '07 #7
On Tue, 18 Dec 2007 15:31:14 +0100, Moi <pa************ @gmail.comwrote :
Hello Rick,

On 18 déc, 06:52, "Rik Wasmus" <luiheidsgoe... @hotmail.comwro te:
<?php
$str="aabcc";
$pattern="/((a+)b?(c+))/";
preg_match_all( $pattern,$str,$ matches);
print_r($matche s[0]);
?>
The behaviour I expect from the above would be to match:
a
aa
c
cc
abc
aabc
abcc
aabbcc
The output is ALWAYS the maximum strings :

What do you mean by 'maximum'? I see nothing weird here...

OK,what I don't understand is why "a" is not captured as (a+) means
"a" or repeated "a"...
because /(a+)b?(c+)/ means:
At least one or more a
Optionally followed by one b
Followed by at least one or more c

If 'a' is taken as a single string, it will not match because there's no c
after it, if it was part of a total string you gave us above, it will
still not match because both 'a' and 'aa' are not followed by 'c' or ('b'
and some 'c's), they're followed by a newline character (\n).
--
Rik Wasmus
Dec 18 '07 #8
Moi
Thanks Rik, I guess that makes sense.
P
Dec 18 '07 #9
..oO(Patrick Drouin)
>Hello Michael,
>Nope. What's returned is the entire matched string and all parenthesized
sub strings (if there are any), but not every single matching point from
during the execution.

The above is exactly what you told preg_match() to return:

Well OK, let me rephrase then, how can I tell PHP to match the
substrings. In my mind, (a+) means a, aa, aaa, ... and not only the
maximum string. I don't see how that behaviour is logical in any way.
That's how regular expressions work in general. The only thing that you
can control in many regex engines is whether the engine should stop the
matching process after it has found a minimum match (ungreedy) or if it
should continue until the maximum length (greedy), which is usually the
default.

Micha
Dec 18 '07 #10

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

Similar topics

6
4801
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search string. It's actually the input string into a Microsoft Index Server search. The string will consist of words, perhaps enclosed in quotes or parentheses. I'd like to use Regex to pull out the words, or the phrases if the words are enclosed in quotes....
20
8121
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 matches are called within a loop (like if or for). E.g. for(int i = 0; i < 10; i++) { Regex r = new Regex();
17
3982
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 http://forta.com/books/0672325667/
7
2233
by: Mike Labosh | last post by:
I have the following System.Text.RegularExpressions.Regex that is supposed to remove this predefined list of garbage characters from contact names that come in on import files : Dim _dropContactGarbage As New Regex( _ "(+)|" & _ "(+)|" & _ "(+)|" & _ "(+)|" & _ "(+)|" & _
9
2092
by: jmchadha | last post by:
I have got the following html: "something in html ... etc.. city1... etc... <a class="font1" href="city1.html" onclick="etc."click for <b>info</bon city1 </a> ... some html. city1.. can repeat lot of times here.... Requirement: ------------------- I want to get the value of "href" i.e "city1.html" by searching "city1" between the <a</atag. Please note that "city1" can repeat lot of
4
2648
by: Chris | last post by:
Hi Everyone, I am using a regex to check for a string. When all the file contains is my test string the regex returns a match, but when I embed the test string in the middle of a text file a match is never returned. The string that I give to the regex is one that contains the entire contents of a text file. I'm using the multi-line option and I've also tried stripping out the VbCr
7
2590
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward slash then some numbers. For some reason I am not getting that. It won't work at all in 2.0
6
4216
by: Phil Barber | last post by:
I am using Regex to validate a file name. I have everything I need except I would like the dot(.) in the filename only to appear once. My question is it possible to allow one instance of character but not two or more? example myfile.doc = good My.file.doc = not good if you could give an example of the expression pattern that would most helpful. thanks phil
1
12214
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 is that I seek out terms which are in a glossary on our site, and automatically link to this definition. Its slightly complex becase certain elements have to be ignored, for exampleI dont want to add links within existing links, or for example link...
0
1632
by: Support Desk | last post by:
That’s it exactly..thx -----Original Message----- From: Reedick, Andrew Sent: Tuesday, June 03, 2008 9:26 AM To: Support Desk Subject: RE: regex help The regex will now skip anything with an '@'in the filename on the assumption it's already in the correct format. Uncomment the os.rename line
0
10507
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...
1
10255
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
9092
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
7582
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
6815
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5473
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5607
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2948
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.