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



  • 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



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



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



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



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



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


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



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



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

    Here are my solutions :

    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



  • 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


Log in to reply