Does PHP support regex shorthand character classes?

e.g.
\d is for digits 0-9
\w is a word
\b is word boundary
etc

I tried simple regex functions in php using these and they didn't work as expected.

<?
echo ereg_replace("\d", "digit", "This is a 7 and a 9.");
?>

should print: This is a digit and a digit. But it just prints the original string.

    Actually, your code will print:

    "This is a 7 andigit a 9."

    I'm not familiar with the "classes" you mention, but I'm only conversant with two or three languages. They look slightly more like PCRE's, but your code doesn't seem to work with [man]preg_replace/man either, so I'm guessing it's some other convention outside PHP and friends.

    This does work:

    <?
    echo ereg_replace("[0-9]", "digit", "This is a 7 and a 9.");
    ?>

    "[:digit:]" works too, as POSIX requires. You'll need some PERL refugee to give you the equivalent PCRE syntax. πŸ˜ƒ

      Okay that is what I assumed. And I was aware of the [0-9], I was just looking for the lazier way! πŸ˜ƒ

      Is there an equivalent for word boundaries (\b)?

      And I see how using the word 'class' here can cause some confusion; I was meaning the regex classes like these.

        Ah! RTFM and found it!

        <?
        echo preg_replace("/\d/", "digit", "This is a 7 and a 9.");
        ?>

        Gotta escape them little suckers....!

          BTW, those are, as the author states, PERL compatible regular expressions (PCRE's). Not as difficult as I once thought. Oh, Lord, no, though; I don't wanna learn PERL.... :rolleyes:

            So we added:

            "/\b/"

            But what exactly are they escaping?

              They're not. They're pattern delimiters. Read up on the subject (information can be found in the manual's chapters on the [man]PCRE[/man] functions). (There; fixed πŸ™‚.)

                Heh, I spent five minutes trying to google PRCE when php.net displayed an error for your link. Now I see it was just a typo.

                [man]PCRE[/man]. Thanks for the reading though. πŸ™‚

                Edit: I'm also guessing that ereg won't work with PCRE, and I'll have to use preg instead (I don't see a difference anywaysπŸ™‚).

                  Write a Reply...