• Login
Community
  • Login

RegEx and Replace..

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
5 Posts 3 Posters 483 Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E
    Eddie Adolfsson
    last edited by Oct 10, 2019, 5:14 PM

    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

    1 Reply Last reply Reply Quote 0
    • A
      Alan Kilborn
      last edited by Oct 10, 2019, 5:19 PM

      This might give you a solid start:

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

      1 Reply Last reply Reply Quote 2
      • E
        Eddie Adolfsson
        last edited by Oct 10, 2019, 6:02 PM

        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?

        A A 2 Replies Last reply Oct 11, 2019, 8:13 AM Reply Quote 0
        • A
          AdrianHHH @Eddie Adolfsson
          last edited by Oct 11, 2019, 8:13 AM

          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.

          1 Reply Last reply Reply Quote 3
          • A
            Alan Kilborn @Eddie Adolfsson
            last edited by Oct 11, 2019, 12:22 PM

            @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.

            1 Reply Last reply Reply Quote 2
            1 out of 5
            • First post
              1/5
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors