Notepad++ regex – numbers

notepadregex

I tried to search for it and experiment it out, but I couldn't figure it out.

In notepad++ I have a line like:

102.

or

12

and I would like to delete the whole line (so i would need \n\r I guess) and all of these kinds of lines, how could I do that? (I don't know if it is a one decimal, two decimal or three decimal or if it has a point after it or not).

Best Answer

I'm not 100% clear on what your requirements are (that is, what the possible contents of the lines to be deleted are) but I've made a regex going off of the assumption that the line starts with between 1 and 3 numbers, followed by an optional full stop, and without anything else on the line (beginning or end).

Find what : ^\d{1,3}\.?\r?\n?

Replace with : (blank)

Tested on the following dataset

102.
102
12
12
1.
1
9999
ab21.
8442.

which leaves behind

9999
ab21.
8442.

Explanation

^ is a special character that matches the very start of the line only. This is just to make sure that the pattern isn't simply on the end of a longer line.

\d is used to match any numerical value (that is, 0-9).

{1,3} is used to modify the previous statement (in this case, \d), and is used to specify the minimum and maximum number of times that you want that statement to match. Therefore, this line says that you want to match a number between 1 and 3 times (inclusive).

\. is used to match the dot character. . is a special character in regex, used to match any character (except a newline by default, although there is an option in Notepad++ to change this behaviour). Due to this, we need to escape it with a backslash to make sure that it is taken as . "the character" and not . "the matching pattern".

? is used to modify the last statement similar to {1,3}, but this time it is used to say that the previous statement ([.]) is optional (technically it says to match it between 0 and 1 times, but the end result is the same).

\r?\n? is used to match the new line as you mentioned already. The regex would work without this, but it wouldn't remove the line, only clear it (leaving a blank line behind). By making both \r and \n optional, this will become portable across Windows, Linux, and Mac.

Finally, the reason that we leave the Replace with : field blank is simply because of the fact that we don't want anything to go back into the line that we're deleting.

Hopefully this is what you were looking for and, if not, points you in the right direction.

Related Question