Search code examples
javaregextibco-business-works

How to extract last X digits from fixed Y digit number using RegEx?


How to extract last X digits from fixed Y digit number using RegEx?

input : 1234567890123456

  • ((?=\d{16}$)\d{10}|) gives 1234567890

But

  • ((?=\d{16}$)\d{10}$|) does not give 7890123456

basically, I want last 10 digits if it is compulsory 16 digit no else nothing.


Solution

  • Looks like you're not using Java and cannot use capture groups.

    For your tibco tool you can use this regex with validation of 16 digits to grab last 10 digits:

    (?<=^\d{6})\d{10}$
    

    RegEx Demo

    RegEx Breakup:

    • (?<=^\d{6}) is a positive lookbehind to assert that we have 6 digits from start to previous position
    • \d{10}$ matched 10 digits at the end of string