RegEx and Replace..

  • I’ve a file with alot of lines in them with various data, but what I’m looking to do is to grab all text containing +99(87654321) and i understand i can just use \d{11} actually but the problem is that when i do, i dont know how to replace any non-qualifying text with empties? So say the file looks like …:
    123 455 61 +9901020304 blah blah 901

    I wish to blank everything but the column of numbers prefixed with +99

  • This might give you a solid start:

    Search for ^(?-s).*?(\+\d{10}).* and replace with \1.

  • Thank you so much, but I forgot to mention that the same document has lines in it that doesn’t have the +99 thing at all in them, and i need those gone too, what do i do then?

  • For similar tasks I adopt a two step process, first to separate the text I want from the rest by putting them on separate lines, second by selecting the (un)wanted lines.

    Step 1: Replace (\+\d{10}) with \r\n\1\r\n.
    Step 2: Mark all lines matching ^\+\d{10}$
    Step 3a: Either Menu => Search => Bookmarks => copy all marked lines, so they can be pasted into another buffer.
    Step 3b: Or Menu => Search => Bookmarks => Remove unmarked lines.

  • @Eddie-Adolfsson

    Just informationally, you can remove lines NOT containing a specific bit of text via finding it with the following method:

    Say you want to match lines that DON’T contain abc. Then your search expression might be ^(?-s)(?!.*abc).*\R. If you replace with nothing when you run a replacement you effectively remove those lines.

    Obviously you can replace abc with whatever you need. It doesn’t have to be constant text, it can be a regular expression of course.

Log in to reply