By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,369 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,369 IT Pros & Developers. It's quick & easy.

Regex with a '*' in it?

P: n/a
The following code gives this warning, any suggestions?

Warning: preg_match(): Compilation failed: nothing to repeat at
offset 0 in test.php on line 6

=========================

$match = '* Hello World';
$test = '* Hello';

if ( preg_match ( "/$match/", $test ) ) echo "Matched";

May 14 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
At Mon, 14 May 2007 13:20:26 -0700, dr_phill123 let his monkeys type:
The following code gives this warning, any suggestions?

Warning: preg_match(): Compilation failed: nothing to repeat at
offset 0 in test.php on line 6

=========================

$match = '* Hello World';
$test = '* Hello';

if ( preg_match ( "/$match/", $test ) ) echo "Matched";
Escape the * (\*) so it loses it's special meaning inside the regex.
Sh.
May 14 '07 #2

P: n/a
dr*********@yahoo.com wrote:
The following code gives this warning, any suggestions?

Warning: preg_match(): Compilation failed: nothing to repeat at
offset 0 in test.php on line 6

=========================

$match = '* Hello World';
$test = '* Hello';

if ( preg_match ( "/$match/", $test ) ) echo "Matched";
* means "repeat previous character". In your expr, * goes first, hence
the warning. What are you trying to find with this expression?

--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
May 14 '07 #3

P: n/a
$match = '* Hello World';
$test = '* Hello';
if ( preg_match ( "/$match/", $test ) ) echo "Matched";
The * is a special character in regular expressions, but here you want to
use it as a normal one. So it must be escaped: '\* Hello World'. And I think
you mixed up the parameters. This should work:

$pattern = '* Hello';
$subject = '* Hello World';

if ( preg_match( '/' . preg_quote( $pattern, '/' ) . '/', $subject ) )
{
echo 'Matched';
}

The function preg_quote will escape all special characters, including the
delimiter (as specified by the second argument). If the pattern is not
variable, however, you can, of course, simply write

$pattern = '/\* Hello/';
$subject = '* Hello World';

if ( preg_match( $pattern, $subject ) )
{
echo 'Matched';
}

Greetings,

Thomas
May 14 '07 #4

P: n/a
Thomas Mlynarczyk wrote:
The * is a special character in regular expressions, but here you want to
use it as a normal one. So it must be escaped: '\* Hello World'. And I think
you mixed up the parameters. This should work:
Thanks that is a good solution. Another one I discovered is to use \Q
as
in. The \Q escapes the whole string. I just remembered it from my
perl
days...

if ( preg_match ( "/\Q$match/", $test ) ) echo "Matched";

May 14 '07 #5

P: n/a
Also sprach dr*********@yahoo.com:
The \Q escapes the whole string.
if ( preg_match ( "/\Q$match/", $test ) ) echo "Matched";
I did not know that. But it's not easy to find in the PHP manual. Thanks for
the hint.

Greetings,

Thomas
May 15 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.