Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A regex using word boundaries (\b) is driving me nuts ... any takers?

2 views
Skip to first unread message

Jo Jo S

unread,
Nov 6, 2009, 2:45:11 AM11/6/09
to
Hi,

The following code outputs nothing, and after reading all the docs I
can still don't understand why.

Can anyone explain why word boundaries before the $ don't seem to be
matching?

Cheers,

Jo

<?php

$applePrice = '$5 per bunch';
$orangePrice = 'One bag $2';

$pattern = '/\b\\$/is';

if (preg_match($pattern, $applePrice))
{
echo "Apple Match!\n";
}


if (preg_match($pattern, $orangePrice))
{
echo "Orange Match!\n";
}

?>

"Álvaro G. Vicario"

unread,
Nov 6, 2009, 4:14:59 AM11/6/09
to
Jo Jo S escribi�:

I'd say the blank space is not a word boundary. Perhaps you mean:

$pattern = '/\b \\$/is';

--
-- http://alvaro.es - �lvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programaci�n web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

Heiko Richler

unread,
Nov 6, 2009, 6:16:14 AM11/6/09
to
Jo Jo S wrote:
> $applePrice = '$5 per bunch';

> $pattern = '/\b\\$/is';

This pattern looks for a word boundary followed by one backslash that
are at the end.

> if (preg_match($pattern, $applePrice))
> {
> echo "Apple Match!\n";
> }

To match a word (something between word boundaries) you may use this.
$pattern = '/\b.*?\b/is';
The question mark after the asterisk tells the search shall not be
greedy. It will consume the minimum of symbols to match the rule not as
many as possible.

You could even match a dollar $ followed by at least one digit:
$pattern = '/\$\d+/is';


--
http://portal.richler.de/ Namensportal zu Richler
http://www.richler.de/ Heiko Richler: Computer - Know How!
http://www.richler.info/ private Homepage

Curtis Dyer

unread,
Nov 6, 2009, 8:49:31 PM11/6/09
to
On 06 Nov 2009, Heiko Richler <heiko-...@nefkom.net> wrote:

> Jo Jo S wrote:
>> $applePrice = '$5 per bunch';
>
>> $pattern = '/\b\\$/is';

The problem is most likely with what the regex engine considers a
word. The assertion likely failed because "$" is not considered
part of a word. You might consider rethinking your approach to
something like this.

$pattern = '/(?:^|\W+)\$/';

Also, notice I dropped the `/i' and `/s' modifiers, as they serve
no purpose at all in your pattern.

> This pattern looks for a word boundary followed by one backslash
> that are at the end.

No, that's not the case. The escaped backslash is escaped for
PHP, not the regex engine. Although the extra escape for PHP is
not necessary here, it is *not* incorrect.

$php_str = '/\\$/';

Regex engine sees:

/\$/

Situations where you need to be aware of both PHP and regex
escaping can get tricky.

<snip>

--
Curtis Dyer
<? $x='<? $x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>

0 new messages