REGEX! Argh!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • The Plankmeister

    REGEX! Argh!

    I'm sure this should work:

    echo preg_replace("/(\w)(\!w)(\w)/","\\1\. \\3","this is.a test.to see. if
    it. works");

    It should output:

    this is. a test. to see. if it. works

    But it doesn't. The string is unaltered. What am I doing wrong?


  • Andy Hassall

    #2
    Re: REGEX! Argh!

    On Wed, 24 Dec 2003 00:28:02 +0100, "The Plankmeister"
    <plankmeister_N OSPAM_@hotmail. com> wrote:
    [color=blue]
    >I'm sure this should work:
    >
    >echo preg_replace("/(\w)(\!w)(\w)/","\\1\. \\3","this is.a test.to see. if
    >it. works");
    >
    >It should output:
    >
    >this is. a test. to see. if it. works
    >
    >But it doesn't. The string is unaltered. What am I doing wrong?[/color]

    What is \!w ? Did you want \W (non-word character)?

    Did you really want to replace with \. instead of .? (. is not special in the
    _replacement_).

    $1 is the preferred syntax over \1 for matches (particularly within regexes
    when used as backreferences because of the ambiguities with character codes).

    Perhaps:

    echo preg_replace("/(\w)(\W)(\w)/",
    "$1. $3",
    "this is.a test.to see. if it. works");

    --
    Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
    Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

    Comment

    Working...