This is because you have a hyphen ( - ) placed in the middle of a character class without escaping.
In the [] character class, you can put a hyphen ( - ) as the first or last character. If you place the hyphen somewhere else, you need to escape it ( \- ) so that it can be matched.
"ab BN123-4.56".scan(/BN([0-9_\.-]+)/)
Note You also do not need to avoid periods ( . ), So you can rewrite this as.
"ab BN123-4.56".scan(/BN([0-9_.-]+)/)
Or even the following if you decide to place it in the middle of a character class.
"ab BN123-4.56".scan(/BN([0-9.\-_]+)/)
source share