Jump to content

Annoying Regex ...


hmm213

Recommended Posts

OK, so I need a regex that will chop the / off the end of a domain name ( if it exists ), leaving the rest of the domain intact...

 

mydomain.com/ -> mydomain.com

mydomain.com -> mydomain.com

mydomain.com/myfile/ -> mydomain.com/myfile

 

etc.... I think you get the picture

 

So far I have this ...

 

    preg_match('/(.*)[\/]+$/s',$google_text,$matches);

 

    $matches[1];

 

The problem is though - it leaves "domain.com/myfolder/myfile" blank ...

 

Anyone got any suggestions ?

 

THanks

Link to comment
Share on other sites

not to denigrate effigy response, his relies on the / being at the end, i wondered if you wanted a more global approach

 

haystack

this is a test mydomain.com/ with diff mydomains some with slashes some without mydomain.com and some with domain.com/myfile/

 

regex

/([\/])(?![[:alpha:]])/

 

result

this is a test mydomain.com with diff mydomains some with slashes some without mydomain.com and some with domain.com/myfile

 

code

$output = preg_replace('/([\/])(?![[:alpha:]])/','','this is a test mydomain.com/ with diff mydomains some with slashes some without mydomain.com and some with domain.com/myfile/');

Link to comment
Share on other sites

Your regexp was good the problem was you put the "0 or more" quantifier ("*") which is why you've been getting empty strings sometimes. But this is easily solved in your case by anchoring the string at the beggining with "^" (you've anchored it at the end with "$"). It should look like this:

 

/^(.*)[\/]+$/s

Link to comment
Share on other sites

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.