Menu

how do I use a counter with regex?

Wolf
2009-03-15
2012-11-13
  • Wolf

    Wolf - 2009-03-15

    Hi all

    I would like to replace all occurances of "; " with the following:
    ; 1
    ; 2
    ; 3
    and so on.

    How can that be done?

    Wolf

     
    • Fool4UAnyway

      Fool4UAnyway - 2009-03-15

      Who says you need (or can) use regex for that?

      There are other ways. You may want to search the forums for "increment" and possibly something like "line number".

       
      • Greg

        Greg - 2009-03-15

        I agree with Fool4U, I don't think RegEx in any of its manifestations can insert incremental numbers in the Replacement string. The only variables you can have in the Replacement strings are ones gleaned from the Search For string.

        If this is really important to you there is a solution but it's complex and challenging if you're nor familiar with TextFX plugin. Here's the general solution.

        Use TextFX (Viz) to hide all the lines that do NOT contain semicolon (;).
        Use TextFX (Tools)  to add line numbers to the lines remaining.
        You will now have lines like 000013 xxxx;xxxxx
        Use TextFX to un-hide lines.
        Use regular expressions to manipulate the ';' lines with something like:

        Search For: ^([0-9]+) ([^\;]*)\;(.*)^
        Replace with: \2; \1 \3

        Customise to fit what you really want.

         
        • Fool4UAnyway

          Fool4UAnyway - 2009-03-15

          I don't think this is going to work, as Hide Lines actually does nothing more than that: it _hides_ the lines, but doesn't keep them from being edited as well. I just did a quick test and when I inserted line numbers, all lines even became visible again. Yes, all had a line number inserted.

          Please, also for replyers: look if there already is some kind of a solution, before "inventing" a new one, that may not even work.

           
          • Greg

            Greg - 2009-03-17

            Wolf,

            Given Fool4U's experimentation with my first general solution, I would refine it as follows:

            Use TextFX to add lines numbers to the whole file.
            Save this as LineNumberCopy.
            Use Find & Replace (Ctrl/H) to delete lines without a semicolon (;)
              Search For: ^.*;.*$
              Replace With: <nothing>
            Save this file as SemicolonLines.
            Open LineNumberCopy again. This time delete lines with a semicolon (;)
              Search For: ^[^;]+$
              Replace With: <nothing>
            Save this file as NonSemicolonLines.
            (I hope you will agree that if SemicolonLines is merged with NonSemicolonLines and line are sorted, you should have your original file because the line numbers mark each line's position in the file.)
            Open the SemicolonLines file.
            Use TextFX to add line numbers again. (I hope this works.)
            Now use the technique I gave before to move the line number at the left to a position just after the semicolon. I'm sure you can make the necessary adjustments.
            (You should now have a file of the semicolon lines with their first line numbers showing at the start of the line.)
            Now you merge this file with the NonSemicolon file (Copy and Paste?) and use the TextFX Sort command to reorder the lines to their original order. (Click the Sort Ascending command to get Sort Ascending.)
            If this looks right, remove the line numbers with the appropriate TextFX line removal command.

            If you work through this, you really do have too much spare time on your hands. :-)

            Don't apologise for your English, it was very clear. I should apologise for my lack of German(?).

            Auf Wiedersehen!

             
            • Fool4UAnyway

              Fool4UAnyway - 2009-03-17

              Again, my suggestion is to read the thread I referred to before.

              It contains an elaborate description for a similar case.
              It also deals with Text FX Insert Line Numbers.
              It is already there.

              There is no need to come up with a new solution for the same problem. By the way, did you test it, (with) the Hide Lines feature?

               
              • Greg

                Greg - 2009-03-18

                Directing his comment at Gr3gW, Fool4U wrote:
                "Again, my suggestion is to read the thread I referred to before. "

                F4U, Why would I do that? I don't have the problem, Wolf66 does.

                Wolf asked the whole readership for help, I responded with what I would do if I had his problem. I didn't claim to have the authoritative answer.

                F4U continued:
                "There is no need to come up with a new solution for the same problem. "

                I agree but, as the 'solutions' are not documented in an easily accessible or understandable form, what else is an exasperated user to do? I thought the Help forum was for users needing help and everyone's contributions were welcome.

                F4U finished with:
                "By the way, did you test it, (with) the Hide Lines feature?"

                No, I took your word for it. When I gave the first general solution, I was *generalising* not giving specific line by line instructions, even if it looked like it. ;-)

                 
              • Fool4UAnyway

                Fool4UAnyway - 2009-03-17

                Suggested reading:

                "Preparing numbers to be sorted by TextFX sort" (Plugin Development forum)
                http://sourceforge.net/forum/forum.php?thread_id=2011755&forum_id=482781

                "FEATURE REQUEST: Insert start/end of line" (Help forum)
                https://sourceforge.net/forum/forum.php?thread_id=2582472&forum_id=331754

                "Help deletin dupe words/lines and sorting" (Open Discussion forum)
                https://sourceforge.net/forum/forum.php?thread_id=2105317&forum_id=331754

                 
                • Greg

                  Greg - 2009-03-18

                  Ahh! At last, some references. Thank you F4U. Not everyone has the same expertise as you in digging out material from the Forum archives.

                  The five references are most welcome. What I would like to see is a Npp Advanced Editing Guide where past solutions are included for specific problems. The index/TOC would be the solutions (eg how to add sequential numbers to specific lines in a file). This should be built over time using the Help forum to identify problems to be solved and references to the solutions in the forum.

                  F4U, you are obviously the resident expert on regular expressions and TextFX, perhaps you would consider taking on the role. Just collect problems and solutions as you go over say the next three months.

                  Once we have an easily accessable Guide, the number of repeated enquiries will surely drop.

                   
                  • Fool4UAnyway

                    Fool4UAnyway - 2009-03-18

                    > F4U, you are obviously the resident expert on regular
                    > expressions and TextFX, perhaps you would consider taking
                    > on the role. Just collect problems and solutions as you go
                    > over say the next three months.

                    As I already answered in a personal email to you: I do not feel like taking on this role. I provide help on these forums and I am certainly not gonna do something just because you ask me (and keep asking me).

                    I suggest, if it is so important to you, you take on this role. The same stuff is as readible and collectable to you as well. Perhaps then you will get what you keep asking for.

                    I really don't see why you would come up with yet another solution, even looking like a line by line instruction but not being it. If you would have looked at the references, you would have seen line by line instructions. It is as simple as that. I do not see why you would have to write another message, not being line by line instructions, but being elaborative as well, possibly not even explaining what it does or why it works (and even without testing it yourself).

                    A lot of information is already on these forums. This information is available. Even if you don't come to think of searching for it yourself, you could simply have clicked the link to get the search results I provided earlier. I asked about this in another thread as well: look before you ask and look before you answer. There is no need to increase the amount of messages returned by the search engine, if already one of them contains a workable solution.

                     
              • Fool4UAnyway

                Fool4UAnyway - 2009-03-17

                Here's more to work through:

                "Find Character "n" and replace "n+1"" (Help forum)
                http://sourceforge.net/forum/forum.php?thread_id=2203593&forum_id=331754

                "Plugin: NPPTextFX" (Plugin Development forum)
                https://sourceforge.net/forum/forum.php?thread_id=1352598&forum_id=482781

                 
      • Fool4UAnyway

        Fool4UAnyway - 2009-03-15

        Take a look at these search results:

        http://sourceforge.net/search/index.php?words=%28%2Bincrement%29&sort=posted_date&sortdir=desc&offset=0&group_id=95717&type_of_search=forums&pmode=0

        Or jump directly to this thread with quite an extensive instruction about how to do a similar thing:

        "Preparing numbers to be sorted by TextFX sort" (Plugin Development forum)
        http://sourceforge.net/forum/forum.php?thread_id=2011755&forum_id=482781

        My quicknote instruction at the moment would be:
        1. Just create as many lines as you eventually want
        2. Use column block select mode to select a column on all these lines: select all lines, press down Alt while still holding the Shift button, press CursorUp and CursorDown to select a column of 0 character width.
        3. Use the column editor Alt+C to enter the desired numbers
        4. Fix up all lines with the desired text (";")

        You might perform the last step first, as well.

         
    • Wolf

      Wolf - 2009-03-17

      Many thanks for the links and ideas provided. It will take a while to digest all that.

      @ Fool4UAnyway:
      > Who says you need (or can) use regex for that?
      Nobody I know of. I found a way to get a batch file to do the job in the meantime. All I wondered was weather there is a way to use Notepad++'s replacement function by ways of regex. I guess there is not.

      @Greg:
      > I don't think RegEx ... can insert incremental numbers in the Replacement string.
      That was the information I was looking for.
      > Here's the general solution. ...
      I will try to work with that and post a reply how it went, as Fool4UAnyway seems to have tested it without success.

      @All:
      Reading my original post on a new day, I came to realize the shortcomings of the information provided. So here is the project that I am working on in greater detail:

      I would like to "tune" my Sokoban Level Collection in a way, where every puzzle is preceded by a line that contains a semicolon, a space and a number that refers to the position of that particular puzzle in the collection. Big Deal. Bored out of my mind, it seems. Sorry for bothering you in the first place with that.
      I took it as a reason to start reading about regular expressions and I have since used reg.expr in Notepad++ a lot. So I at least have learnt something.

      Thank you both very much for your time and efforts.
      For completeness, one more message will be posted by me, in case anybody wants to know. But I have to do that tomorrow.

      Wolf

      PS.: sorry for my bad english

       
    • cchris

      cchris - 2009-03-17

      For this sort of programmatic text manipulation, I keep an old DOS editor, Bingo, which I use a couple time a year, I'd say. It features not only regexes, but a script language called Chess - it looks like a sybset of ANSI C with an interface to the editor's internals - which you can use to script actions on text held by the editor. Perhaps some day the NppJavaPlugin by F. Fesevur will allow the same level of flexibility using a conventional language, but it is still far from that.

      A plugin to implement Chess would be very handy, but I'm much more familiar with Pascal or Eiffel than with C++, so I'm wary of writing it myself.

      CChris

       
    • Wolf

      Wolf - 2009-03-18

      @ Greg,

      1.)
      As Pointed out by Fool4UAnyway your first suggestion (message from 2009-03-15 23:00) inserts line numbers everywhere. (promised completeness satisfied)

      2.)
      > Use Find & Replace (Ctrl/H) to delete lines without a semicolon (;)
      > Search For: ^.*;.*$
      > Replace With: <nothing>

      This replaces lines with semicolons, not without them. At this point I will stop testing more solutions to my problem using regular expressions, because my batch file works like a charm.

      But I would like to say that this discussion has given me quite some inspiration for future problems that I might come across. So a final thank you to Greg for your time and efforts.

      @ Fool4UAnyway,
      thanks for digging out the references given above. To read AND understand their contents will definately take some time. But now I have access to them.

      @ CChris,
      a search for the DOS based Bingo editor came up with a series of dead links. Is it still available somewhere? This might not be appropriate to ask in this forum, so my appologies here, just in case. :-)

      Wolf

       
    • Wolf

      Wolf - 2009-03-18

      Post Scritum:

      I don't know how you guys did your replies, but I found it quite interesting to see that messages to this thread don't get listed in chronological order. I guess I can reply to individual messages somehow, with the added feature of losing space (the text area get more and more narrow).

      I do not want to suggest any different behaviour of forum software or forum users. Just a thought occured to me that other newcomers to this forum could be confused as well particularly by messages out of sync (not chronological). I will have to read the entire thread again, in case new messages pop up.

      I hope that you will not bear any animosities against me saying so.

      Wolf

       
      • Greg

        Greg - 2009-03-18

        Yes, Wolf, I've noticed the same thing. I can't work out how the forum tool decides where to position the reply-to message. It's certainly not date order, unless it is something to do with international time being handled incorrectly.

        When you see a list of message titles, that is called Thread View. If you come to the thread from the top level starting at Help forum , then what you see is not a Thread View but all messages expanded. Clicking on one of the message titles will change the view to Thread View. I haven't yet found a way to go back to a 'Show All' view.

        Probably something we just have to live with.

         
        • Fool4UAnyway

          Fool4UAnyway - 2009-03-18

          Each subthread is moved down, according to the last posted reply.

          So it was possible for one Edit message of Wolf to appear first, having me puzzled what he was talking about, and the original post below it, because that, or any sub-message in its own sub-thread had been replyed to later.

           
          • Fool4UAnyway

            Fool4UAnyway - 2009-03-18

            To be more correct: each individual message in the "root" of the thread is moved down when it gets its first reply (and perhaps by any later reply directly to that message). Each reply in the sub-thread doesn't move the sub-thread itself down, but I guess the same thing happens for each individual or nested message / sub-thread in the sub-thread.

             
    • Wolf

      Wolf - 2009-03-18

      Edit:

      I hope that you will not bear any animosities against me FOR saying so.

      Wolf

       
    • cchris

      cchris - 2009-03-18

      Wolf,

      The following has a ton of text editors. I don't know whether all links are alive, but the one for be400b.zip is.

      http://www.eunet.bg/simtel.net/msdos/editor-pre.html

      I got this page by googling for "bingo text editor".

      CChris

       
    • Wolf

      Wolf - 2009-03-18

      CChris,

      your link works, Thank you.

      Wolf

       
      • Fool4UAnyway

        Fool4UAnyway - 2009-03-18

        When you click on the title of each individual message in a thread, you get to the individual message, showing all messages in the same thread below it.

        You can now "Post a followup to this message" below this thread message listing.

        I always have some problem with not the complete thread content being visible when replying to individual messages. It would be convenient to be able to do so from the complete thread view in the first place instead of "Add a new message to this thread".

         
  • Fool4UAnyway

    Fool4UAnyway - 2010-07-09

    I didn't want to directly overload you with probably mostly unuseful information about regular expressions and when to (not) use them.

    A simple search for "regex number" took me to this thread:

    "how do I use a counter with regex?" (Help Forum)
    http://sourceforge.net/projects/notepad-plus/forums/forum/331754/topic/3104460

    Well, counters in regex isn't (currently) possible, but you may read the complete thread or jump to the threads referenced in it, dealing with numbers.

    \d is just a shortcut for  which means any character in the range from 0 to 9
    + means "any consecutive string of the preceding character(s) with at least one"

    You may also check the Text FX Find/Replace Dialog (Ctrl+R). It has a short dropdown list in the upper right corner showing a couple of elementary expressions.