1

I need to delete all lines on a file non starting by "07". I already try a lot of regular expressions without success. can someone pleas ehelp me with this?

jpmd
  • 247
  • 1
  • 7
  • 19
  • Show us the code you have tried. Also, if you are doing this in `bash` or the `shell`, then you should add the tag. We have no idea what language you are using because the tags and code are missing. – jww May 01 '14 at 07:53
  • possible duplicate of [Regex: remove lines not starting with a digit](https://stackoverflow.com/questions/1826311/regex-remove-lines-not-starting-with-a-digit) or [Delete all lines starting with # or ; in Notepad++](http://stackoverflow.com/questions/5185983/delete-all-lines-starting-with-or-in-notepad) – jww May 01 '14 at 07:55
  • 1
    @jww: No, the "not" in this question is significant. – T.J. Crowder May 01 '14 at 07:56
  • Got it, thanks. I added the not-starting duplicate to the "possible duplicate". – jww May 01 '14 at 07:59

3 Answers3

1

The regex is

^(?!07).*$

using the g (global) and m (multiline) flags. Search on that and replace with nothing.

Link with explanation:

  • The ^ asserts "beginning of line" (because we used the m modifier).

  • The (?!07) means "not starting with 07". It's called a "negative look-ahead".

  • The .* means "zero or more of anything".

  • The $ means "end of line" (because we used the m modifier).

  • The g modifier means "globally" (all occurrences).

I haven't used Notepad++, but the docs say it uses PCRE, so the above should work. If it expects you to write the expression and flags all together, that would most likely be:

/^(?!07).*$/gm
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • +1 But to remove _whole lines_, I'd modify the pattern to include an optional EOL too: `^(?!07).*$(?:\r?\n)?`. As-is the pattern above leaves empty lines in the wake of the replacements. – ridgerunner May 01 '14 at 14:10
  • @ridgerunner: Thanks. As I say, I've never used Notepad++, wasn't sure how it would interpret the end-of-line stuff. Seems to have worked for the OP, though. – T.J. Crowder May 01 '14 at 14:17
0

Just do this:

^(?!07).*

and replace with nothing.

You don't need $ because .* stops at new line.

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
-1

i think you can use a match like this

/^(0[^7]).*/gm

in this case it will match for any number but not 7

since [07] will match for 0 or 7 not both

Sirius_Black
  • 471
  • 3
  • 11
  • No, that leaves lines that don't start with a `0`. – T.J. Crowder May 01 '14 at 08:04
  • can you give me an example so i can fix it? /(^0[^7]).*/gm – Sirius_Black May 01 '14 at 08:14
  • @ Sirius: See my answer, it has the correct rex and a complete explanation. – T.J. Crowder May 01 '14 at 08:25
  • @T.J.Crowder yes, i just asked an example, i made an edit and posted on comments – Sirius_Black May 01 '14 at 08:32
  • @ Sirius: I'm afraid I'm not following. That's okay, I don't need to. :-) If you want to know the correct way to do this, that's posted above. I suggest deleting this answer as it's incorrect. – T.J. Crowder May 01 '14 at 08:51
  • i just asked an example of how it wouldnt match, i didnt ask for the correct way, you can suggest as many time as you want, instead of downvoting the answer... i dont care – Sirius_Black May 01 '14 at 08:56
  • @ Sirius: There's no reason to be rude. I simply misunderstood your question. Here's an example of a line that would be deleted incorrectly: `0mumble`. As for the downvote, two lessons you need to learn **early** if you're going to be happy contributing on SO: 1. People downvote without commenting, and comment without downvoting. If you assume you know who downvoted something, you will be wrong *dramatically* more often than right. 2. Downvotes are not personal. They're quality control. An answer that is incorrect attracts downvotes, that's what they're for. – T.J. Crowder May 01 '14 at 09:03