Move Controller



  • Ok so I have this regex that removes all the spaces on the first line

    So in my txt file my first line has a { I’m not sure how to use proper spacing here so I am going to use - to position my example

    -----{
    —Code
    —Code
    —Code

    So what I have moved the { all the way back like this
    {
    —Code
    —Code
    —Code

    But what I would like to find is a move controller, if I want to move the { 4 spaces forward, or move it back, if it can be based on rows maybe, because I have many { don’t want to move the wrong ones



  • @Javier-Rivas tried to add my regex,
    So here it is
    Search: (?-s)\A(.?)\x20+(?s)(.)
    Replace with: \1\2



  • @Javier-Rivas said in Move Controller:

    Search: (?-s)\A(.?)\x20+(?s)(.)
    Replace with: \1\2

    Not sure why you think you need to include the remainder of the file with the \2 replace part. Just don’t look for it in the Search field, then you don’t need to write it back. Since the \A is used the regex will ONLY work on the first line.

    In terms of your question in the first post, unsure of how to position characters on the line in the example. We like posters to put examples inside of the black boxes (hopefully you have seen them on some of the posts. To do so, insert examples, then select them and click on the </> button above the posting window. This effectively stops the markdown engine (behind the posting window) from altering the text in the box. It gives the people helping you more certainty that the examples are unaltered.

    So in terms of your first post, how about trying to include the example prior to change made, and then include it again (in a second box) with the changes. It gives us a better chance of seeing what you want to do. You talk of many rows, so do you need to alter the { on other rows, how do you determine which rows?

    Terry



  • @Terry-R ok. So I can’t edit my post. For some reason it says I have to wait for 3 minutes,
    Original position of the {

         {
      Code 1
      Code 2
      Code 3
      {
      Code 4
      Code 5
      },
    

    Where it lands with that regex I posted

    {
      Code 1
      Code 2
      Code 3
      {
      Code 4
      Code 5
      },
    

    Were it needs to land

      {
      Code 1
      Code 2
      Code 3
      {
      Code 4
      Code 5
      },
    


  • @Javier-Rivas said in Move Controller:

    Were it needs to land

    Thanks very much for putting the examples inside the 2 black boxes. It makes it so much simpler to understand.
    So if you need to alter EVERY { so it is the same number of spaces in as the following code this regex should do it
    Using the Replace function we have
    Find What:^\x20+?\{(\R)(\x20+)(\w)
    Replace With:\2{\1\2\3

    If however it is ONLY the first line in the file, then use the \A at the start of the regex in place of the ^.

    So how you get on with that.

    Terry



  • @Terry-R perfect I will test, so the first line is \A, is \B the second line and so on



  • @Javier-Rivas said in Move Controller:

    so the first line is \A, is \B the second line and so on

    No, the \A ONLY refers to the first line of a file. There is nothing for the 2nd, 3rd or other lines like this. To find other lines it means using a regex like:
    Find What:(?-s)(.+?\R){n}
    where n = 1 less then the line you want. So if wanting the 3rd line n=2. So after the regex has captured 2 lines the cursor is now at the start of the 3rd line.

    Is there a need to exactly hit specific lines? Possibly further examples might help explain what you need.

    Terry



  • @Javier-Rivas said in Move Controller:

    if it can be based on rows maybe, because I have many { don’t want to move the wrong ones

    What you said (above) suggests to me that you have many locations in the file where the { is NOT aligned with the following code and want to fix that. You don’t need to know the row number in advance. Have a try of this regex in the “Find” function.
    Find What:^(\x20+?)\{(\R)(?!\1\w)
    Click on the “Count” button.
    Then replace that regex with this
    Find What:^(\x20+?)\{(\R)(\x20+?)\w
    and click on “Count” again.

    The second number is how many { exist in the file with the next line containing code. The first number is how many { exist which are NOT aligned with the following code.

    Hopefully looking at these regexes you can see we don’t need to know which row the data is on, the regex finds it according to the codes we supply. The first regex ^(\x20+?)\{(\R)(?!\1\w) means:
    ^(\x20+?) start of line and capture as many spaces as exist
    \{ as the { is also a special (metacharacter) character I delimit it to say I want the character, not the meaning behind it
    (\R) capture the end of the line
    (?!\1\w) look at the next few characters and we don’t want exactly the same number of spaces followed by code. This is the “IF” part you would see in a program code. This regex ONLY looks for a following line with code where the number of spaces is different.

    Terry



  • @Terry-R no specific reason, just trying to learn how this works incase spacing is wrong, I have over 10 { and 10 }, and none are aligned which is how it needs to be, but I do have other files that do need { to be all aligned, so only for this task the first line is needed, what I do is merge 2 or more files together and this is what happens. Main focus is the first { and that it’s aligned with last } from the other files

                {
        Code 1
        Code 2
        Code 3
        },
                 {
        Code 1
        Code 2
        Code 3
        },
    
    

    With my first regex after it’s used, and after I merge my files. Each file starts with { and ends with },

    {
        Code 1
        Code 2
        Code 3
        },
    {
        Code 1
        Code 2
        Code 3
        },
    
    

    What I do is I put the cursor behind the first { and hit the space bar 4 times to move it forward, that aligns it
    That’s why I’m trying to understand the regex I posted to better control it.
    Instead of moving the { all the way back. Could a space counter be added, let’s say I want to move the first { 4 spaces back only, or I use my regex and then use another to move the first { 4 spaces forward to get it to align, I have over 10,000 files all with the same problem



  • @Terry-R thank you for your time, It’s 1 am here, so I’m off to bed. I hope my last comment helps to understand my questions, once again the best help ever, I tried other community sites for different languages and man I was tossed out many times. With my 3 posts. I’ve been at these problem for months and all this time notepad ++ had the answers.

    Hey is their a way for notepad ++ to save my find and replace actions in to a short cut, for example I find code 1 and replace it with code A, and I save that command, so that I don’t always type it in the find and replace, or mark



  • @Javier-Rivas said in Move Controller:

    Could a space counter be added, let’s say I want to move the first { 4 spaces back only, or I use my regex and then use another to move the first { 4 spaces forward to get it to align, I have over 10,000 files all with the same problem

    Have you tried my solution
    Using the Replace function we have
    Find What:^\x20+?\{(\R)(\x20+)(\w)
    Replace With:\2{\1\2\3
    It will find ANY{ followed by code on the next line, then align the { the same as the code.

    You refer to 4 spaces, is that because the code has 4 spaces as well. And is it ONLY the first line. If only first line and always 4 spaces then use this
    Using the Replace function we have
    Find What:\A\x20+?{(\R\x20+\w)
    Replace With: {\1 there are 4 spaces at the front

    At the moment I think I’ve supplied as much information as I can and you haven’t clarified much, except to say you have lots of files.

    Need to know

    1. Is it ONLY first line to change in each file?
    2. Does the { always need 4 spaces in front or does it need to be aligned with the next code line?
    3. Can the code line have more or less than 4 spaces in front?

    Your last question can be solved by saving the regex as a macro. We can talk more on that once we solve your immediate problem.

    Terry



  • @Terry-R said in Move Controller:

    the \A ONLY refers to the first line of a file. There is nothing for the 2nd, 3rd or other lines like this. To find other lines it means using a regex like:
    Find What: (?-s)(.+?\R){n}
    where n = 1 less then the line you want

    I might point out that strictly the regex won’t find the (absolute) n th line of the file, but rather it will find the (relative) n th line from the current position.

    Ticking Wrap around changes this. With that the start of the file is where the search begins. Somewhat obviously an “all” search (e.g. Find All in Current Document) also begins a search at start-of-file.



  • @Alan-Kilborn said in Move Controller:

    strictly the regex won’t find the (absolute) n th line of the file, but rather it will find the (relative) n th line from the current position.

    You are right. It want intended as an answer but just trying to show some other ideas. I suppose I could have added that it wasn’t a complete solution.

    Terry



  • @Terry-R thank you, I will try everything that was shared, I have tried to explain, but the focus is only the first line with the first { that needs to move, on other documents I will need the find all {, but in this post it’s only the first that needs to move, I will put together everything that was shared and post back with what did the job



  • @Terry-R - Thank you once again for the help, but I couldn’t get these to work

    I tried as is and nothing happen
    Find What:^\x20+?\{(\R)(\x20+)(\w)
    Replace With:\2{\1\2\3
    
    I tried as is and nothing happen
    Find What:\A\x20+?{(\R\x20+\w)
    Replace With: {\1
    

    I tried to rebuild one using the explanations and the regex offered, and the only one that is responding is the one I shared

    for some reason only this one works, but it removes all the spaces, and I only need it to move certain spaces, or add certain spaces on the first line behind my {
    Search: (?-s)\A(.?)\x20+(?s)(.)
    Replace with: \1\2
    

    Can some tell me what each sections means, this is what I think it means

    (?-s) - This means any number of spaces to reduce
    \A - This means line 1
    (.?)
    \x20+(?s) - This means the number spaces exist
    (.)
    
    

    I don’t know how the ( . ) fits in this, and why don’t I see that in the other regex offered
    I feel like if this code could be altered to count and reduce spaces from \A, that would be easier

    I would still like to know how this code works, I found a solution to my problem
    I am using a Plugin called

    ToolBucket
    

    this helps me match the position I need with the find a replace and I can have over 500 txt files opened, and it will edit all my files and position the { correctly

    Thank you everyone



  • @Javier-Rivas said in Move Controller:

    Can some tell me what each sections means, this is what I think it means

    Since you had supplied an initial regex there was an assumption you had some knowledge of regular expression coding. It would seem you have none.

    the \A actually means “start of file”. It is a zero width position immediately at the start, before ANY characters. It is used as an anchor in a regex.

    The . means ANY character with the exception of the CR and LF which are seen at the end of each line, although in some Mac or Unix files this can be either one or other, Windows generally uses both. In Notepad++ under the Replace function there is an option to tick (select) called . matches newline which allows the . to also match CR and LF characters (carriage return and line feed). The . is called a metacharacter so if wanting an actual . it needs to be shown as \., the \ is a delimiter.

    The (?s) is equivalent to the option called . matches newline. This allows a regex to select during processing which mode it needs to be in, regardless of the option being selected or not. That mode ONLY works on regex code after it. So you can have a mixture of (?-s) and (?s) in the regex. There are some regex solutions on this forum where this has been used.

    So my original solution was:
    ^\x20+?\{(\R)(\x20+)(\w)

    This means:
    ^\x20+? start of line followed by as many spaces as exist together, or none (?).
    \{ refers to an actual {. Since this is a metacharacter I used the delimiter to confirm it’s the character I want, not the metacharacter.
    (\R)(\x20+) means a CR and/or LF, (newline commands) followed by as many spaces as exist together.
    (\w) means a word character, so 0…9, a…z, A…Z plus a few others which need not concern you currently. This is used to confirm that the next line actually has some code in it, not just a blank line or another { line.

    If you want to truly understand and write regexes you should read the FAQ section and locate the manuals on Notepad++, regular expressions and try some exercises. This is not the place to be conducting a training session.

    So if you have a PlugIn which solves your needs then we have no need to try and solve it for you?

    Terry



  • @Terry-R Thank you, I will look at that, and as I was posting my last comment, I found that plugin and it did what I needed, I will practice regex, and see if I can build something

    Once again thank you


Log in to reply