problem with pattern

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
enne87
Forum Newbie
Posts: 2
Joined: Wed Aug 20, 2008 10:07 am

problem with pattern

Post 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
Corvin
Forum Commoner
Posts: 49
Joined: Sun Dec 03, 2006 1:04 pm

Re: problem with pattern

Post by Corvin »

Possible solution:

Code: Select all

$str = "r099c33";
$str = preg_replace("/r(.*)c(.*)/", "\\1", $str);
echo $str;
enne87
Forum Newbie
Posts: 2
Joined: Wed Aug 20, 2008 10:07 am

Re: problem with pattern

Post by enne87 »

Perfect, thx corvin.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: problem with pattern

Post 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
Post Reply