As for (.*?)\s*$...
parens are used to ask the regular expression to remember the contents of it, for later use, whether inside or out of the engine. In this case, out; as we've used it to create the values set denoted by $matches[2][0..n]
. - the period is a metacharacter that will match any character.
* - the asterix is a metacharacter that tells the engine to look for zero or more of the preceeding data. In this case, that's any character.
? - the question mark is a metacharacter that typically tells the engine to find zero or one of the preceeding data. In the case where it follows a greedy pattern metacharacter like *, it attempts to find the shortest, or least greedy match that still works with the rest of the pattern.
\s* - as I said before, look for zero or more (greedy) whitespaces.
$ - this metacharacter, when placed at the end of the pattern denotes the end of line.
so basically, this pattern looks for any length of characters up to whitespaces that are followed by the end of line. So if you have any number of words/letters/characters, the whitespaces and end of line will be excluded from memory.