pghtech
pghtech

Reputation: 3702

How can I use a Regular Expression to return a number of numbers at the end of a string of digits

I have a string of digits that could vary in length and I want to return only the last 4 digits. Would I use a positive lookback? And use the $ to anchor to?

Upvotes: 1

Views: 92

Answers (2)

Julien Ch.
Julien Ch.

Reputation: 1261

If your string can be ANY length and you want to match the cases when you have 0 to 3 charaters you should use:

\d?\d?\d?\d?$ 

or, if the regex engine understands it:

\d{0-4}$

Upvotes: 0

burning_LEGION
burning_LEGION

Reputation: 13450

use this regular expression \d{4}$

Upvotes: 4

Related Questions