Basically you have two ways you can go about this:
You can use a regular expression to match the strings you described and capture the portion which you want to store in a variable.
You can split your strings into arrays on the underscore separator, and do comparisons against the last elements of those arrays to see whether they are in your list of items to match. If so, store the appropriate (second) element of the array.
Neither is particulary complicated, but the second might be easier for you if you have no knowledge of regexes.