regexregex-lookaroundslookbehind

Regex match digits with lookahead


I have a regex:

(\d{1,20})

Which matches:

JPY;       7112630;

In this string I want to catch only the digits: so I use: $1 But I also want to add ".00" after the digits so I use: $1.00

But now I also want to match the digits, only if I have JPY before.

I tried:

(?=JPY;\s*)(\d{1,20})
(?<=JPY;\s*)(\d{1,20})

No success!


Solution

  • You may use a capturing group instead of the infinite-width positive lookbehind:

    (JPY;\s*\d{1,20})
    

    And replace with $1.00. See the regex demo.

    Depending on the regex engine, you may remove the (...) capturing parentheses, and use $0 or $& instead of the $1 in the replacement pattern.

    Also, if you need to match these values as whole words, enclose with word boundaries: \b(JPY;\s*\d{1,20})\b.