Community
    • Login

    RegEx and Replace..

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    5 Posts 3 Posters 477 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.
    • Eddie AdolfssonE
      Eddie Adolfsson
      last edited by

      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
      • Alan KilbornA
        Alan Kilborn
        last edited by

        This might give you a solid start:

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

        1 Reply Last reply Reply Quote 2
        • Eddie AdolfssonE
          Eddie Adolfsson
          last edited by

          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?

          AdrianHHHA Alan KilbornA 2 Replies Last reply Reply Quote 0
          • AdrianHHHA
            AdrianHHH @Eddie Adolfsson
            last edited by

            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
            • Alan KilbornA
              Alan Kilborn @Eddie Adolfsson
              last edited by

              @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
              • First post
                Last post
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors