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
https://forums.phpfreaks.com/topic/81112-annoying-regex/
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
https://forums.phpfreaks.com/topic/81112-annoying-regex/#findComment-412127
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
https://forums.phpfreaks.com/topic/81112-annoying-regex/#findComment-413035
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.