Help with regex

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

Moderator: General Moderators

Post Reply
dreamline
Forum Contributor
Posts: 158
Joined: Fri May 28, 2004 2:37 am

Help with regex

Post by dreamline »

Hi All,
I'm not that good in regex so I was hoping someone could help me out with my following problem. Suppose I have the following strings:
test.test1.
test2.test3.test3.

What I want to achieve is a regex that replaces the first few strings up until the before last ., so that I will have test1. and test3. (including the dots at the end) to work with.

Thanks for any help on this. :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Help with regex

Post by Christopher »

You want to use preg_match_all() with the PREG_OFFSET_CAPTURE flag. Remember that '.' is a wildcard in regex, so you want to search for '\.' Or you could just use string functions like:

Code: Select all

$str = substr($str, strrpos($str , '.', -1) + 1);
(#10850)
dreamline
Forum Contributor
Posts: 158
Joined: Fri May 28, 2004 2:37 am

Re: Help with regex

Post by dreamline »

Thanks Christopher, that'll do the trick. :)
Post Reply