Is there a way to group and apply function to different wildcards?

I’ve got metrics like the following:

carbon.agents.ip-*-*.metricsReceived

The first wildcard distinguishes different ip addresses. The second wildcard distinguishes different port numbers (multiple ports for each ip).

For each ip, I’d like to sum together the metric values of its ports and display it as a single value. Do this for each ip and the end result would be a graph that has several different lines, each one representing the total incoming metrics for that ip.

Is there a way to do this?