• Login
Community
  • Login

split line help requested

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
splitnewline
4 Posts 3 Posters 229 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.
  • M
    Matt Whitlock
    last edited by Matt Whitlock May 14, 2021, 4:15 PM May 14, 2021, 4:13 PM

    recipes.addShaped("aetherworks:prism", <aetherwork
    recipes.addShaped("aetherworks:prism_support", ul
    recipes.addShaped("animania:banner", 
    recipes.addShaped("animania:banner_alt"
    recipes.addShaped("aquaculture:iron_fishing_rod",
    

    I am sure this will be really easy for someone, but I have searched google for 20 minutes without any luck.
    I just want to add a new blank line after every time the first word after the first quote mark changes from the above, to this:

    recipes.addShaped("aetherworks:prism", <aetherwork
    recipes.addShaped("aetherworks:prism_support", ul
    
    recipes.addShaped("animania:banner", 
    recipes.addShaped("animania:banner_alt"
    
    recipes.addShaped("aquaculture:iron_fishing_rod",
    

    thanks

    P 1 Reply Last reply May 14, 2021, 8:38 PM Reply Quote 1
    • P
      PeterJones @Matt Whitlock
      last edited by May 14, 2021, 8:38 PM

      @Matt-Whitlock ,

      I am sure this will be really easy

      Not “really easy” for me, though I was able to figure it out; it just took some time. (There are others here who are better at regex than I am; I was hoping one of them would step in, but since they hadn’t yet, I thought I’d put a few minutes in now that I found a little bit of time.)

      • FIND = (?x-s) \(" (\w+) .*\R\K(?= .*? \(" (?!(\1)) \w+ )
      • REPLACE = \r\n$0
      • Search Mode = regular expression

      “Quick” Explanation:

      • (?x-s) = allow extra spaces in the regex for easier readability; make sure . doesn’t match newline
      • \(" = Match a literal ( and a literal "
      • (\w+) = grab a word and put it in memory as group#1
      • .*\R = eat up the rest of the line, including its EOL sequence
      • \K = reset the match, so the replacement won’t try to delete/change that first line
      • (?= ...) = this is a lookahead assertion , so what’s in here has to match, but again won’t be part of the full match
      • .*? = this gobbles up the first part of the new line
      • \(" = match a literal ( and a literal " on the second line as well
      • (?! ... ) = a negative lookahead, which says whatever comes next cannot match what’s inside
        • (\1) = this matches the same value as what was in group 1
        • since it’s in the negative lookahead, that means that this will only be true if what comes next does not match what’s in group#1
      • \w+ = this is the word (which because of the negative lookahead must be a different word that the value of group#1)

      After those are all combined, the actual match is actually just the 0-width position at the beginning of each line whose word doesn’t match the previous line’s word.
      It is replaced by a \r\n, which is the windows CRLF newline sequence; if you are using linux EOL, just use \n there.

      I added a couple of extra test cases based on your description, because I wanted to make sure that it properly handled words who were only on one line.

      original data:

      recipes.addShaped("aardvark:animal", blah
      recipes.addShaped("abcdefgh:alphabet", blah
      recipes.addShaped("aetherworks:prism", <aetherwork
      recipes.addShaped("aetherworks:prism_support", ul
      recipes.addShaped("animania:banner", 
      recipes.addShaped("animania:banner_alt"
      recipes.addShaped("aquaculture:iron_fishing_rod",
      

      transformed:

      recipes.addShaped("aardvark:animal", blah
      
      recipes.addShaped("abcdefgh:alphabet", blah
      
      recipes.addShaped("aetherworks:prism", <aetherwork
      recipes.addShaped("aetherworks:prism_support", ul
      
      recipes.addShaped("animania:banner", 
      recipes.addShaped("animania:banner_alt"
      
      recipes.addShaped("aquaculture:iron_fishing_rod",
      

      (My first implementation didn’t have the lookahead on the second half, which meant it consumed that line without grabbing its word, and thus it didn’t put a blank line between abcdefgh and the first aetherworks. I was glad I had added the two single-line words, otherwise I wouldn’t have seen that bug.)

      You did a good job presenting before and after data, so kudos for that. But I’ll present my normal hints for getting regex help, since there are helpful links. And some of the advice may also help you with asking an even better question next time. (“… If there is a next time.”)

      ----

      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.

      1 Reply Last reply Reply Quote 2
      • G
        guy038
        last edited by guy038 May 15, 2021, 1:31 AM May 15, 2021, 1:30 AM

        Hello @matt-whitlock, @peterjones and All,

        An other solution, based on the same idea than @peterjones, with a positive look-ahead, containing itself a negative look-ahead searching for a key-word different from the previous one !

        SEARCH (?-si)^.+?"(.+):.+\R\K(?=[^"\r\n]+"(?!\1:))

        REPLACE \r\n

        Tick the Wrap around option and click on the Replace All button only ( Do not use the Replace button )

        I assume that there is only one : character per line, right after the KEY-WORD to look at !


        BTW, Peter no need to add $0, in the replacement part, as it represents an empty string, in that specific case ;-))

        Best Regards

        guy038

        1 Reply Last reply Reply Quote 1
        • M
          Matt Whitlock
          last edited by May 16, 2021, 6:09 PM

          Thank you for the replies friends. <3

          1 Reply Last reply Reply Quote 1
          1 out of 4
          • First post
            1/4
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors