This is a discussion on regular expression within the PHP Language forums, part of the PHP Programming Forums category; Hi Gurus, How can I write the thing below as a regular expression (preg_replace)? $array = array("-0", "-...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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($array, "", $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 |
|
|||
|
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($array, "", $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.... http://regularexpressions.info -- Rik Wasmus |
|
|||
|
On Jan 20, 6:18 pm, "Rik" <luiheidsgoe...@hotmail.com> wrote:
> 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($array, "", $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); > > http://regularexpressions.info > > -- > 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 |