Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex: remove lines not starting with a digit

I have been fighting this problem with the help of a RegEx cheat sheet, trying to figure out how to do this, but I give up... I have this lengthy file open in Notepad++ and would like to remove all lines that do not start with a digit (0..9). I would use the Find/Replace functionality of N++. I am only mentioning this as I am not sure what Regex implementation is N++ using... Thank you

Example. From the following text:

1hello
foo
2world
bar
3!

I would like to extract

1hello
2world
3!

not:

1hello

2world

3!

by doing a find/replace on a regular expression.

like image 239
Peter Perháč Avatar asked Dec 01 '09 13:12

Peter Perháč


2 Answers

^[^\d].* marks a whole line whose first character is not a digit. Check if there are really no whitespaces in front of the digits. Otherwise you'd have to use a different expression.

UPDATE: You will have to do ot in two steps. First empty the lines that do not start with a digit. Then remove the empty lines in extended mode.

like image 62
moxn Avatar answered Nov 15 '22 16:11

moxn


You can clear up those line with ^[^0-9].* but it will leave blank lines.

Notepad++ use scintilla, and also using its regex engine to match those.

\r and \n are never matched because in Scintilla, regular expression searches are made line per line (stripped of end-of-line chars).

http://www.scintilla.org/SciTERegEx.html

To clear up those blank lines, only way is choose extended mode, and replace \n\n to \n, If you are in windows mode change \r\n\r\n to \r\n

like image 26
YOU Avatar answered Nov 15 '22 15:11

YOU