Home > Software engineering >  Regex to remove strings in notepad that contains certain amount of numbers
Regex to remove strings in notepad that contains certain amount of numbers

Time:12-29

If I have the following lines, how to

  1. leave only 10-11 digits long strings that starts with "04" or "05". Don't remove empty lines.
0409999999  012345678
012345678   0409999999
023456789   034566  0455555555
012345678   012345678
0299999999

so the lines above should then look like:

0409999999
0409999999
0455555555


CodePudding user response:

\b(?!04|05)\d{1,9}\b|\b\d{10,11}\b

CodePudding user response:

I would suggest pattern

\b\d{0,9}|0[45]\d{8,9}\b

Explanation:

\b - word boundary

\d{0,9} - match up to 9 digits

| - alternation

0 - match 0 literally

[45] - match 4 or 5

Regex demo

EDIT

After update you can use [ \t]*(?!0[45]\d{8,9})\b\d [ \t]*

The difference here is that it uses negative lookahead to assure that what is ahead is not 10-11 digit number starting with 04 or 05.

[ \t] are used to trim space and tabs.

Then you just need to replace it with empty string.

  • Related