• Login
Community
  • Login

Regex for deleting a number in a line only if it is the only one

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
10 Posts 4 Posters 728 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.
  • G
    glossar
    last edited by Dec 6, 2020, 8:53 AM

    Hi all,

    I’m looking for a regex that will find a number in a line which contains no other number - namely only “1.” if there is no “2.”, “3.”, and so on, in the same line.

    Example:
    entry1 [tab] 1. definition 2. another definition 3. another one
    entry2 [tab] 1. definition
    entry3 [tab] 1. definition 2. another definition 3. another one 4. another one

    It should find only the “1.” in the second line above, the one with entry2. This number (“1.”) may or may not follow a tab character [e.g. “ethyl alcohol tab Chem. 1.” ] and may or may not be followed by only one space.

    Mant thanks in advance!
    glossar

    1 Reply Last reply Reply Quote 0
    • T
      Terry R
      last edited by Dec 6, 2020, 9:10 AM

      @glossar said in Regex for deleting a number in a line only if it is the only one:

      if there is no “2.”, “3.”, and so on, in the same line.

      I don’t think it even needs a regex. If every line which contains multiple definitions includes a 2, at a minimum then use the “Mark” function and also tick bookmark lines. The search mode can be normal and type in 2,. Click on Mark All. Then look for “remove all non bookmarked lines” (under bookmark? sorry not on PC right now).

      You should be left with lines that have multiple definitions only.

      Terry

      1 Reply Last reply Reply Quote 0
      • T
        Terry R
        last edited by Terry R Dec 6, 2020, 9:17 AM Dec 6, 2020, 9:17 AM

        @Terry-R said in Regex for deleting a number in a line only if it is the only one:

        Then look for “remove all non bookmarked lines” (under bookmark? sorry not on PC right now).

        Sorry I may have misread your question. Do you want to remove the line, which is my solution or JUST the 1. in that line. If just the number then you could cut those non bookmarked lines elsewhere and then remove using a simple regex. Then move back, although they will be out of original order.

        Sometimes a simple multi-part solution is easier, especially when the user needs to understand the process and support it going forward.

        Terry

        G 1 Reply Last reply Dec 6, 2020, 10:14 AM Reply Quote 2
        • G
          glossar @Terry R
          last edited by Dec 6, 2020, 10:14 AM

          @Terry-R said in Regex for deleting a number in a line only if it is the only one:

          If just the number then you could cut those non bookmarked lines elsewhere and then remove using a simple regex. Then move back, although they will be out of original order.

          Terry

          Hi Terry,
          Thanks for the effort.
          I’m tidying up the entries, putting things in order, and deleting junks (in this case the number “1.” since it is useless/unnecessary to number a single item (definition)). I’ve not compeleted the task yet, so the order of the entries is currently important. I assume I can’t put back those lines, cut with the Bookmarks function, in the same place. It would hence be nice if this is performed in place (in the same line) with a regex or any other function of Notepad++ itself.

          After the operation, the result should look like this:

          entry2 [tab] definition

          Thanks,
          glossar

          T A 2 Replies Last reply Dec 6, 2020, 10:42 AM Reply Quote 0
          • T
            Terry R @glossar
            last edited by Dec 6, 2020, 10:42 AM

            @glossar said in Regex for deleting a number in a line only if it is the only one:

            I assume I can’t put back those lines, cut with the Bookmarks function, in the same place.

            It is possible but takes a lot more steps. It involves adding line numbers at start of line, then removing them later.

            Someone should be able to conjure up a regex for you. I would if I were at a PC. Currently late in evening so it will have to be someone else.

            Terry

            1 Reply Last reply Reply Quote 0
            • A
              Alan Kilborn @glossar
              last edited by Dec 6, 2020, 12:01 PM

              @glossar

              Well, this seems to work, but it may be “overcomplicated”:

              find: (?-s)^.*?\K\d+\. (?:(?:.*?\d+\.(*SKIP)(*F))|(?:(?!.*?\d+\.)))
              repl: nothing
              action: Replace All

              It will turn this text:

              entry1 [tab] 1. definition 2. another definition 3. another one
              entry2 [tab] 1. definition
              entry3 [tab] 1. definition 2. another definition 3. another one 4. another one
              

              into this text:

              entry1 [tab] 1. definition 2. another definition 3. another one
              entry2 [tab] definition
              entry3 [tab] 1. definition 2. another definition 3. another one 4. another one
              
              G 1 Reply Last reply Dec 6, 2020, 12:21 PM Reply Quote 2
              • G
                glossar @Alan Kilborn
                last edited by Dec 6, 2020, 12:21 PM

                @Alan-Kilborn
                Many thanks! Much appreciated. It works like a magic!

                1 Reply Last reply Reply Quote 2
                • T
                  Terry R
                  last edited by Dec 6, 2020, 6:59 PM

                  @glossar said in Regex for deleting a number in a line only if it is the only one:

                  It should find only the “1.” in the second line above, the one with entry2.

                  I have this solution. Easier (on the eye) than @Alan-Kilborn’s solution and it seems to work as expected.

                  Replace function
                  Find What:1\.\h(?!.+?2\.)
                  Replace With: empty field here

                  So it says look for a 1. and the rest of the line cannot contain a 2.. So it assumes with a multi definition line the numbers will ascend gracefully, i.e. 1., 2., 3., 4. etc. So any line that DOES have the 2. will not be altered. As there is no replacement field the 1. and a following space are removed.

                  Terry

                  1 Reply Last reply Reply Quote 2
                  • G
                    guy038
                    last edited by guy038 Dec 6, 2020, 7:51 PM Dec 6, 2020, 7:50 PM

                    Hello, @glossar, @alan-kilborn, @terry-r, and All,

                    Here are my solutions :

                    • The first regex is similar to @alan-kilborn’s one :
                    SEARCH    (?x-s)  ^.*?  \K   \d+\.\x20  ( .+  \d+\.  (*SKIP)(*F) | )
                    
                    REPLACE   Leave EMPTY
                    
                    • The second one does not use backtracking control verbs but ensures that some zones of text do not contain any digit :
                    (?x)  ^entry\d+  ( [^\d\r\n]+ )  \K   \d+\.\x20  (?=(?1)$)
                    

                    Best regards

                    guy038

                    1 Reply Last reply Reply Quote 2
                    • G
                      glossar
                      last edited by Dec 6, 2020, 8:55 PM

                      Hi @guy038 and @Terry-R ,

                      Thank you both for your subsequent solutions. Alan’s @Alan-Kilborn formula have done the job, so I can’t even reproduce the problem now :P, but I’ll note yours just in case I need it again.

                      Thanks again.
                      glossar

                      1 Reply Last reply Reply Quote 2
                      • D dr ramaanand referenced this topic on Feb 7, 2024, 3:25 PM
                      9 out of 10
                      • First post
                        9/10
                        Last post
                      The Community of users of the Notepad++ text editor.
                      Powered by NodeBB | Contributors