'Regex: Extract digits separated by a hyphen

What I have: 123456789-987654321
What I need: 3456789-4321 (keep last 7 digits from 1st group and keep last 4 digits from 2nd group)

(\d{7})-         gets me 3456789-
((\d{4})$(?<=))  gets me 4321

I can't figure out how to combine the 2 to get what I need.



Solution 1:[1]

You can match optional digits and then capture the 7 digits and the hyphen. Then match again optional digits and capture the last 4.

In the replacement use the 2 capture groups.

^\d*(\d{7}-)\d*(\d{4})$

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1