Home > Software engineering >  Notepad replace specific number with up to it's first 4 digit
Notepad replace specific number with up to it's first 4 digit

Time:09-23

I want to find those number which contains more than 5 digits and replace it with first 4 digits. Used below Regex to find number which contains more than 5 digits.

[0-9]{5,}

How Can I achieve blow output?

99999999 -> this will replace with 9999
12345.66 -> this will replace with 1234.66 
1234 -> Remains unchanged

CodePudding user response:

This one should do it:

enter image description here

The regex

([0-9]{4})[0-9] 
  • takes the four numbers as first (and only) group
  • requires at lease one more number behind
  • replaces the complete match with the first (and only) group

CodePudding user response:

Using notepad , you can match 4 digits, then use \K to clear the current output buffer and match 1 or more digits.

\d{4}\K\d 

See a enter image description here

If you don't want partial matches, you can add word boundaries \b around the pattern.

\b\d{4}\K\d \b

See another regex demo

  • Related