The idea is to capture the characters before the 1st 4-digit sequence (as group 1), the 4-digits themselves as group 2, and then the remainder as group 3. The groups are defined by the parentheses '()'. The groups can then be referenced (or 'associated') by its ordinal position, using the character sequence \#, where # is the group ordinal position. For example, \1 refers to the first group.

Note that the first group is (.*?) as opposed to just (.*). The difference is that the former variation will take the first 4-digits found, while the latter will take the last digits found. The ? is a qualifier which tells the regex to not be greedy, so it stops at the first instance. Without the ? qualifier, the regex will continue to look for all subsequent digit sequences too and only stop at the last one.

In your final example, there is a trailing space, but you can just add a separate method to trim spaces.