import re


def groupByCategories(article_list=[]):
    """
    Group articles into article categories

    Category is obtained from the http link in below format:
    1(https://www.nytimes.com/2018/05/04/) 2(movies) 3(/sandra-bullock-mindy-kaling-oceans-8.html)

    2 gives the category
    
    Parameters
    ----------
    article_list : list
        List of Articles 
        
    Returns
    -------
    dict
        Dictionary of articles where key is category and value is list of articles    
    """
    groupedArticles = {}
    for article in article_list:
        url = article['url']
        # split the url into 3 parts
        # eg. 1(https://www.nytimes.com/2018/05/04/) 2(movies) 3(/sandra-bullock-mindy-kaling-oceans-8.html)
        reg_ex = r"([a-zA-Z0-9\.\-_/:]*/[0-9]{4}/[0-9]{2}/[0-9]{2}/)([a-zA-Z0-9]+)(/[a-zA-Z\-\.]*)"
        category = re.split(reg_ex, url)[2]
        if not category in groupedArticles:
            groupedArticles[category] = [article]
        else: 
            groupedArticles[category].append(article)
    return groupedArticles