Jump to content

preg_matching last occurence of string in a string??


Recommended Posts

I'e been Googling and Googling and reading and reading to no avail and would really appreciate any input anyone might have on how I can match the last occurence of "swme-" in the following string (this is one of those times when Google is near useless at least with respect to returning any relevant pages). 

 

$string = "/home/carlos/web/swme-site-generator/swme-contact-me.txt"

 

The preg_match I have been trying to work with doesn't work yet.  It's...

 

preg_match('/\/swme-(.*?\.txt)/U', $string)

 

The problem is that the regex matches "/swme-site-generator/swme-contact-me.txt" and not "/swme-contact-me.txt". 

 

I thought that using the '?' would keep it from being greedy but I guess not. 

 

Anybody got any ideas on how to match only the last bit of the string where "swme-" is found? 

 

I am trying to remove the last "swme-" from the string.

 

Thanks.

 

Carlos

Thanks for moving this to the correct forum Alex.  I had forgotten there was a regexp forum here when I posted it and then couldn't firgure out how to move it myself. 

 

I kinda solved my problem by using the following revised preg_match but it's not really a solution since it limits my file names to only the characters in the character class involved...which is not really disireable.

 

preg_match('/\/swme-([a-z0-9-]*?\.txt)/U', $string)

 

That will return the string I want when I apply a preg_replace using the same regexp in it. 

 

preg_replace('/\/swme-([a-z0-9-]*?\.txt)/U', '/$1', $string) returns "/home/carlos/web/swme-site-generator/contact-me.txt" which is what I am after. 

 

In other words I am looking at removing the last occurance of "swme-". 

 

But now I am stuck having to remember where this regexp is in the code so as to change it every time I want to allow a new character into file names.  Not good. 

 

There must be a more elegant and generic way to remove the last occurance of "swme-" from the string in question.

 

Anybody? 

 

Carlos

Are you just trying to get the file name from the path? If so regex, although it will work, isn't the best solution. You could just do this:

 

$string = "/home/carlos/web/swme-site-generator/swme-contact-me.txt";
echo pathinfo($string, PATHINFO_BASENAME);

 

If you want the preceding forward slash you can just append that.

Thanks Alex but I need the entire path minus the last "swme-".  Too complicated to explain why here but it's part of a CMS system I am creating and I need the full path and base file name to remain. 

 

I mean I could use your approach, strip out the "swme-" from the file name, and then put it all back together again but that seems like a lot of trouble to go through given that there is probably a simple regexp that will accomplish what I want and that I am hoping someone here will point me to. 

 

Carlos

I can't seem to think, and I'm not that great at regex anyway, this will work, but there are probably better solutions.

 

$string = "/home/carlos/web/swme-site-generator/swme-contact-me.txt";
echo strrev(preg_replace('~-emws~', '', strrev($string), 1));

Thanks very much for taking the trouble of figuring that out Alex.  Much, much appreciated.  I thought of doing that myself (in theory) but I am actually after just a standard regexp that might do the trick. 

 

We'll see if anyone else can come up with such.  If not I will probably use your solution. 

 

Thanks again Alex!

 

Carlos

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.