Aaron
Aaron

Reputation: 2755

Regex : Match digits connected by hyphen or space with lookahead of four digits

I already have regex pattern in matching digits connected by hyphens or spaces, however I would like to unmatch or have a lookahead of four digits

e.g

  1. "123232-453253-535-23" match should be "123232-453253-5"

  2. "123-452-3233-2" match should be "123-452-3"

Simply, what I would like to do is to ignore the last four numbers.

My current pattern is

(\d[ \d-]{3,}\d)(?=[ \d-]{4})

demo

now this one does the right thing for the example below. when there is no hyphen or space in the last four characters

e.g 123456789 the match is 12345

however when the string is like this

1234-5678-9 what I get is 1234-56

What I'm tring to attain is that I should get the last four digits not the last four characters

1234-5678-9 the match should be 1234-5 ..

123 4567 89 the match should be 123 45

Upvotes: 0

Views: 1049

Answers (1)

Lucero
Lucero

Reputation: 60190

This might work for you:

(\d[ \d-]{3,}\d)(?=(?:[ -]?\d){4})

Basically, the lookahead is changed so that it matches four (non-capturing) groups of an optional separator and one digit, which I understand is what you are looking for.

Upvotes: 1

Related Questions