I want to match/extract the last 4 consecutive digits in a string. The pattern I need to match is therefore [0-9]{4}
, but only the last instance of it in the string - if there are any non-numbers in between, it should match the next instance instead.
Some examples:
ABC123456789A9876 // should extract 9876
ABCD1234567890F23 // should extract 7890
1234ABCD567JIJLMN // should extract 1234
ABCDEFG1234-!.567 // should extract 1234
How can I accomplish this using regex? Alternatively if there's an easier way to do this programmatically with PHP/Laravel I'd also be open to it.