Page 1 of 1

problem with pattern

Posted: Wed Aug 20, 2008 10:10 am
by enne87
Hi!

I'm totally new to Regex so I apologize if this question may sound ridiculous.
I've got the following string which I want to analyze:

"r099c33"

Now I want all digits between r and c.
Can you tell me which pattern would fit best to this task pls?

Thanks.

enne

Re: problem with pattern

Posted: Wed Aug 20, 2008 12:36 pm
by Corvin
Possible solution:

Code: Select all

$str = "r099c33";
$str = preg_replace("/r(.*)c(.*)/", "\\1", $str);
echo $str;

Re: problem with pattern

Posted: Thu Aug 21, 2008 5:59 am
by enne87
Perfect, thx corvin.

Re: problem with pattern

Posted: Thu Aug 21, 2008 7:25 am
by GeertDD

Code: Select all

substr('r099c33', 1, 3);
I know the above code may look ridiculously simplistic. However, it does the job. Really, it does, and faster than any regex. I am posting it just to make the point that in order to create a good regex you need context.

If you have got a list of codes like below, my substr() will do just fine and regex is overkill.

a789s33
d850k89
z123d8
e058d971