regular expression

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

    regular expression

    Hi Gurus,

    How can I write the thing below as a regular expression (preg_replace)?

    $array = array("-0", "-1", "-2", "-3", "-4");
    $key = str_replace($ar ray, "", $key);

    I am trying to learn regular expressions, but I find them hard and this
    seems like a really simple place to start....

    Cheers

    Nicolaas

  • Rik

    #2
    Re: regular expression

    windandwaves wrote:
    How can I write the thing below as a regular expression
    (preg_replace)?
    >
    $array = array("-0", "-1", "-2", "-3", "-4");
    $key = str_replace($ar ray, "", $key);
    I would normally not use a regular expression for this (str_replace is
    often faster), but here you go:

    preg_replace('/-[0-4]/','',$key);
    I am trying to learn regular expressions, but I find them hard and
    this seems like a really simple place to start....


    --
    Rik Wasmus


    Comment

    • Curtis

      #3
      Re: regular expression

      On Jan 20, 6:18 pm, "Rik" <luiheidsgoe... @hotmail.comwro te:
      windandwaves wrote:
      How can I write the thing below as a regular expression
      (preg_replace)?
      >
      $array = array("-0", "-1", "-2", "-3", "-4");
      $key = str_replace($ar ray, "", $key);
      >
      I would normally not use a regular expression for this (str_replace is
      often faster), but here you go:
      >
      Yes, that's a good point. In some situations it may be better to use
      str_replace, substring, or similar functions.
      preg_replace('/-[0-4]/','',$key);
      >

      >
      --
      Rik Wasmus
      For me, the best way to learn was just trying to apply regex in
      projects I was already doing. At first, I spent a lot of time
      debugging, until I eventually started getting the hang of them (now one
      of my favorite features in a language). Tutorials that demonstrated
      with strings like "aaabc" often made it harder for me to grasp.
      However, http://regularexpressions.info is a pretty good reference

      Curtis

      Comment

      Working...