Get parts of the $host variable in nginx

Let's admit that $host domain is in the form company.project.user.domain.tld and I need to get each part of it and then use somewhere else in the nginx config.

Put inside http section:

map $host $company {
    default google;
    ~*(?<c>[\w]+)(.*)$ $c;
}
map $host $project {
    default api;
    ~*([\w]+)(\.)(?<p>[\w]+)(.*)$ $p;
}
map $host $user {
    default dmitri;
    ~*([\w]+)(\.)([\w]+)(\.)(?<u>[\w]+)(.*)$ $u;
}

log_format alogformat "$time_local [company: $company] [project: $project] [user: $user] $scheme://$host:$server_port$request_uri";
And use in server section this type of logging:
access_log /var/log/nginx/accessdtelinov.log alogformat;

Restart nginx and do a request to the host company.project.user.domain.tld and check the logs.