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

A small regex question

Hello,

This code:

<?php
preg_replace_callback("/(a)|(b)/",
create_function('$x',
'foreach ($x as $y => $z)'.
'echo "\\\$y: $z<br>";'),
"b");
?>

Outputs:

\0: b
\1:
\2: b

I expect 'b' to be placed in \1, but for some reason \1 contains a
null value and 'b' is moved to \2. Can someone please explain why it
happens?

Thank you
Jul 17 '05 #1
3 1692
On 29 Oct 2003 16:00:25 -0800, on*******@hotmail.com (Online) wrote:
This code:

<?php
preg_replace_callback("/(a)|(b)/",
create_function('$x',
'foreach ($x as $y => $z)'.
'echo "\\\$y: $z<br>";'),
"b");
?>

Outputs:

\0: b
\1:
\2: b

I expect 'b' to be placed in \1, but for some reason \1 contains a
null value and 'b' is moved to \2. Can someone please explain why it
happens?


You have two capturing subexpressions; 'b' is in the second one.

Did you mean (a|b) instead?

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #2
> You have two capturing subexpressions; 'b' is in the second one.

Did you mean (a|b) instead?


Yea, I have two capturing brackets, but the pattern is separated by |
into two parts - (a) and (b).

Afaik when (a) fails to match, the regex engine should completely
ignore the rest of the current part and move on to (b) and try to
match it against the text. And if (b) matched, the letter 'b' should
be placed inside \1.

The problem here is that \1 gets a null value while 'b' is pushed to
\2, and I don't understand why it acts this way. On mIRC, that
includes an implementation of PCRE, the letter 'b' is placed in \1 (as
I expect), though on PHP it's moved to \2.

I am aware there are other ways of doing this simple match, like
"(a|b)" or "([ab])", but this problem occured to me with a more
complicated pattern and I think "(a)|(b)" is the simplest form of
presenting it.
Jul 17 '05 #3
Online wrote:
Yea, I have two capturing brackets, but the pattern is separated by |
into two parts - (a) and (b).

Afaik when (a) fails to match, the regex engine should completely
ignore the rest of the current part and move on to (b) and try to
match it against the text. And if (b) matched, the letter 'b' should
be placed inside \1.
No. With a pattern like "(a)|(b)" matching against a string like "b",
$0 contains the entire pattern match; $1 contains the text matched by
the first capturing subpattern (nothing); and $2 contains the text
matched by the second capturing subpattern ("b").

The description of preg_replace's replacement parameter might be
helpful in understanding what's really happening:

| Replacement may contain references of the form \\n or (since PHP
| 4.0.4) $n, with the latter form being the preferred one. Every such
| reference will be replaced by the text captured by the n'th
| parenthesized pattern. n can be from 0 to 99, and \\0 or $0 refers
| to the text matched by the whole pattern. Opening parentheses are
| counted from left to right (starting from 1) to obtain the number
| of the capturing subpattern.

http://www.php.net/manual/en/function.preg-replace.php
I am aware there are other ways of doing this simple match, like
"(a|b)" or "([ab])", but this problem occured to me with a more
complicated pattern and I think "(a)|(b)" is the simplest form of
presenting it.


Those are different to your first pattern. Which do you really want?

--
Jock
Jul 17 '05 #4

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

Similar topics

4
by: engwar1 | last post by:
Not sure where to ask this. Please suggest another newsgroup if this isn't the best place for this question. I'm new to both vb.net and regex. I need a regular expression that will validate what...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
2
by: Tim Conner | last post by:
Hi, Thanks to Peter, Chris and Steven who answered my previous answer about regex to split a string. Actually, it was as easy as create a regex with the pattern "/*-+()," and most of my string...
6
by: Du Dang | last post by:
Text: ===================== <script1> ***stuff A </script1> ***more stuff <script2> ***stuff B
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: Chris | last post by:
How Do I use the following auto-generated code from The Regulator? '------------------------------------------------------------------------------ ' <autogenerated> ' This code was generated...
6
by: Martin Evans | last post by:
Sorry, yet another REGEX question. I've been struggling with trying to get a regular expression to do the following example in Python: Search and replace all instances of "sleeping" with "dead"....
7
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...
6
by: | last post by:
Hi all, Sorry for the lengthy post but as I learned I should post concise-and-complete code. So the code belows shows that the execution of ValidateAddress consumes a lot of time. In the test...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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
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,...

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.