Jump to content

Regex for getting yahoo groups account


basher400

Recommended Posts

hi

I have strings that contain the url of an yahoo group and it may appear in any of the following patterns:

it.groups.yahoo.com/group/batmaras/links/
it.groups.yahoo.com/group/batmaras/links
it.groups.yahoo.com/group/batmaras/
it.groups.yahoo.com/group/batmaras

 

 

I need a regex that can always capture the "batmaras" (I can't guess that part, I need to extract it using regex) and give me a result as follows:

it.groups.yahoo.com/group/batmaras

I want to remove anything after the "batmaras" part.

this is what I tried and is not working for me:

(it\.groups\.yahoo\.com\/group\/.*?)\/?

 

Link to comment
https://forums.phpfreaks.com/topic/295373-regex-for-getting-yahoo-groups-account/
Share on other sites

$urls = array(
    'it.groups.yahoo.com/group/batmaras/links/',
    'it.groups.yahoo.com/group/batmaras/links',
    'it.groups.yahoo.com/group/batmaras/',
    'it.groups.yahoo.com/group/batmaras'
);
 
foreach($urls as $url)
{
    echo "<br><b>INPUT:</b> {$url}<br>\n";
    if(preg_match("#it\.groups\.yahoo.com/group/([^\/]*)#is", $url, $groupMatch))
    {
        $groupName = $groupMatch[1];
        echo "<b>GROUP NAME:</b> {$groupName}<br>\n";
        $groupURL = "it.groups.yahoo.com/group/{$groupName}";
        echo "<b>GROUP URL:</b> {$groupURL}<br>\n";
    }
    else
    {
        //Did not match group name
        echo "<b>No Group Name found</b><br>\n";
    }
}

Results

INPUT: it.groups.yahoo.com/group/batmaras/links/
GROUP NAME: batmaras
GROUP URL: it.groups.yahoo.com/group/batmaras

INPUT: it.groups.yahoo.com/group/batmaras/links
GROUP NAME: batmaras
GROUP URL: it.groups.yahoo.com/group/batmaras

INPUT: it.groups.yahoo.com/group/batmaras/
GROUP NAME: batmaras
GROUP URL: it.groups.yahoo.com/group/batmaras

INPUT: it.groups.yahoo.com/group/batmaras
GROUP NAME: batmaras
GROUP URL: it.groups.yahoo.com/group/batmaras

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.