Regex: Search and Replace / Remove all kind of spaces from the beginning of the line



  • hello. I have this code:

          My cat is very big.
    I love mistery.
    Read everything starting here.
       I love myself.
    

    So, I want to remove all empty spaces at the beginning of line 1 and 4, with search and replace. Of course, it is just an example, my real text is much complicated, also the regex. Anyway:

    FIND: (My cat is very big\.)|(I love myself\.)
    REPLACE BY: \s+\S+ \1\2 or by ^\s+\S+ \1\2

    Is not working.

    The output should be:

    My cat is very big.
    I love mistery.
    Read everything starting here.
    I love myself.


  • @Vasile-Caraus said in Regex: Search and Replace / Remove all kind of spaces from the beginning of the line:

    FIND: (My cat is very big.)|(I love myself.)
    REPLACE BY: \s+\S+ \1\2 or by ^\s+\S+ \1\2
    Is not working.

    Why would you think that would work?! If you want to search for SPACES followed by specific TEXT, and replace with just TEXT, the SPACES need to be in the FIND regular expression, not the REPLACE expression. I cannot fathom how after all this time of being given regex solution after regex solution that you haven’t at least figured out that much.

    As always, you are extremely unspecific about what you really want, and you leave us guessing. You have two requirements listed, and you don’t say which is the real requirement. First, you say “on line 1 and 4”, so does that mean that you always want to remove leading space from lines 1 and 4, no matter what’s on lines 1 and 4? But you also show a FIND expression which lists two specific sentences, so do you really want to remove leading space on any line that contains those exact sentences? Or do you only want to remove leading spaces on lines 1 and/or 4 if lines 1 and/or 4 match the specific text given? All three of those require different regular expressions.

    I will not answer the whole question, because you haven’t actually asked the whole question yet.

    But I will show you the answer for the simple case of “remove leading space on all lines”:

    • FIND = ^\s*
    • REPLACE = (leave empty)


  • @Vasile-Caraus said in Regex: Search and Replace / Remove all kind of spaces from the beginning of the line:

    FIND: (My cat is very big.)|(I love myself.)
    REPLACE BY: \s+\S+ \1\2 or by ^\s+\S+ \1\2

    Ridiculous… please stop wasting the time of everyone that chooses to read postings on this forum.



  • Hello, @vasile-caraus, @peterjones, @alan-kilborn and All :

    Why don’t you use the N++ command Edit > Blank Operations > Trim Leading Spaces ?!


    On the other hand, the Replace syntax \s+\S+ \1\2 or ^\s+\S+ \1\2 are erroneous. Indeed, regexes, used in replacement, are generally different from the regexes used in search part !!

    For instance, when inserted in replacement, the regex ^\s+\S+ \1\2 would simply replace the overall search match with the string ^s+S+ followed with a space char and the contents of the search groups 1 and 2 !

    Best Regards,

    guy038



  • This post is deleted!


  • @PeterJones said in Regex: Search and Replace / Remove all kind of spaces from the beginning of the line:

    FIND = ^\s*
    REPLACE = (leave empty)

    Thank you, sir.



  • @Vasile-Caraus ,

    Here are some takeaways from this problem. Hopefully, you will remember these, and act accordingly in the future:

    1. The documentation at https://npp-user-manual.org/docs/searching/ describes all the proper syntax for searching and replacements. In it, you would find the list of \-escapes that are allowed in the REPLACE WITH field (https://npp-user-manual.org/docs/searching/#substitution-escape-sequences). You will notice that the \s and \S are not valid sequences in that field, as @guy038 tried to explain.
    2. If you want to search for text that contains spaces, the space character or escape sequence must go in the FIND WHAT, not in the REPLACE WITH
    3. The quality of answers you receive is directly proportional to the quality of the question you ask

    Other advice: you have been pointed to the documentation many times over your 5+ years of visiting this forum. You should take advantage of that resource, and read and study that document.

    ----

    Do you want regex search/replace help? Then please be patient and polite, show some effort, and be willing to learn; answer questions and requests for clarification that are made of you. All example text should be marked as literal text using the </> toolbar button or manual Markdown syntax. To make regex in red (and so they keep their special characters like *), use backticks, like `^.*?blah.*?\z`. Screenshots can be pasted from the clipboard to your post using Ctrl+V to show graphical items, but any text should be included as literal text in your post so we can easily copy/paste your data. Show the data you have and the text you want to get from that data; include examples of things that should match and be transformed, and things that don’t match and should be left alone; show edge cases and make sure you examples are as varied as your real data. Show the regex you already tried, and why you thought it should work; tell us what’s wrong with what you do get. Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries.


Log in to reply