13

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.

1

5 Answers 5

23

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

3
  • Oh, okay, I think I will be satisfied by this explanation. Dec 1, 2009 at 14:11
  • 4
    a clever trick I learned later for getting rid of blank lines on Windows: replace \n\r with nothing. That way \r\n\r\n becomes \r\n. Any number of blank likes will be shortened to \r\n. Oct 24, 2012 at 8:15
  • Note the Bookmark answer by Peter Perháč. It was just what I needed when I landed on this page. (I am adding this comment to the accepted answer, as that feature may not have been available in Notepad++ when the question was first answered.)
    – jcadcell
    Jan 23, 2013 at 17:54
8

[^0-9] is a regular expression that matches pretty much anything, except digits. If you say ^[^0-9] you "anchor" it to the start of the line, in most regular expression systems. If you want to include the rest of the line, use ^[^0-9].+.

3
  • also worked but left a whole lot of blank lines. How can I capture the line break too? Dec 1, 2009 at 14:01
  • Have you tried adding [\r\n]* at the end of your expression?
    – PP.
    Dec 1, 2009 at 14:02
  • It looks like this does work only in "extended mode" in np++, but not in regex-mode.
    – moxn
    Dec 1, 2009 at 14:04
8

^[^\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.

3
  • this worked as far as finding all lines not starting with a digit, but when I did a search/replace, searching for ^[^\d].* replacing it with nothing, I am still left with a lot of blank lines. How would I have your regex capture the line break too? Dec 1, 2009 at 13:58
  • You could try first to remove all lines with digits. And then you could switch to the "extended mode". I tested it and it works for me to find linebreaks here with \r\n. Replace them with nothgin then.
    – moxn
    Dec 1, 2009 at 14:06
  • yeah, that's a time-tested way to do it, but I was hoping this could all be done in a single step. Maybe if I were using some other editor, but it's npp, and so I'll be content with doing this in two steps :) cheers Dec 1, 2009 at 14:16
4

One could also use the technique of bookmarking in Notepad++. I started benefiting from this feature (long time present but only more recently made somewhat more visible in the UI) not very long ago.

Simply bring up the find dialogue, type regex for lines not starting with digit ^\D.*$ and select Mark All. This will place blue circles, like marbles, in the left gutter - these are line bookmarks. Then just select from main menu Search -> Bookmark -> Remove bookmarked lines.

Bookmarks are cool, you could extract these lines by simply selecting to copy bookmarked lines, opening new document and pasting lines there. I sometimes use this technique when reviewing log files.

1
  • 1
    Great tip! For removing unmatched lines there's also "Remove Unbookmarked Lines". Often I can easily regex what I want to keep; now it's easy to remove the rest. Thanks!
    – jcadcell
    Jan 23, 2013 at 17:35
1

I'm not sure what you are asking. but the reg exp for finding the lines with a digit at the beginning would be ^\d.* you can remove all the lines that match the above or alternatly keep all the lines that match this expression: ^[^\d].*

2
  • I am trying to figure out which part of my question is not clear. I would edit it, but I think I am quite clear in asking how to remove lines not starting with a digit? Dec 1, 2009 at 14:00
  • what was not clear to me was how are you trying to remove. now I understand you are using search and replace. try to search for ^[^\d].* and replace with \b (which is the backspace character or alternativley *and this worked for me in the past) search for ^[^\d].*\R and replace with nothing (the R must be capital!) <br><br> if the latter is "greedy" as in, it deletes all the lines after the first match then you can try replacing ^[^\d][^\R]*\R woth nothing
    – codekitty
    Dec 1, 2009 at 14:11

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.