{5} and {6} expect specific, exact numbers of matches. We have many more tools to quantify the matches:

? means zero or one (an optional match) * means any number (zero or more) + means at least one (one or more)

We can also use a comma inside {} to make a range of possibilities. (However, we cannot normally put a space in between the {}; that will break the special meaning of the {}.)

{5,6} means either 5 or 6 (between 5 and 6, inclusive. We are not listing each possible option, but instead beginning and end points for a range.) {,6} means up to 6 (between 0 and 6, inclusive) {5,} means 5 or more

So, the ? quantifier means the same thing as {0,1} (or {,1}); * means the same thing as {0,} (in fact we can even use {,}); + means the same thing as {1,}.

A regex looks for the first match. Given _\w{5}|_\w{6}, it’ll try the first alternative (_\w{5}) before the second alternative (_\w{6}), so if there’s a ‘_’ followed by 6 (or more) digits, it’ll match the ‘_’ and the first 5 digits.

The fix is to put the longer match first, i.e. _\w{6}|_\w{5}, or , as others have said, _\w{5,6}.