regular expression

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", "-...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-21-2007
windandwaves
 
Posts: n/a
Default 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($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

Reply With Quote
  #2 (permalink)  
Old 01-21-2007
Rik
 
Posts: n/a
Default 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($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


Reply With Quote
  #3 (permalink)  
Old 01-22-2007
Curtis
 
Posts: n/a
Default Re: regular expression

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

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:26 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0