• Login
Community
  • Login

Merge lines using Regex

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
8 Posts 3 Posters 982 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.
  • F
    Faisal Alam
    last edited by Faisal Alam Dec 11, 2020, 4:41 AM Dec 11, 2020, 4:40 AM

    So i needed text in bulk my texts are in a huge quantity so I can not do every line manually and i do have lots of txt* too. Please try understand with the example given.

    Example:
    My text:

    3040
    Woodsidi
    5202
    Sivilli Rd
    6406
    W 81 ST
    1853
    Yukon Ct
    1130
    BiLT AVi
    5530
    South 5475
    1295
    S. Vrain St.
    

    What i want:

    3040 Woodsidi
    5202 Sivilli Rd
    6406 W 81 ST
    1853 Yukon Ct
    1130 BiLT AVi
    5530 South 5475
    1295 S. Vrain St.
    

    I am able to find and select the text from my own RegEx but unable to merge it
    My RegEx:

    ^\d.{3}\r\n^[A-Z]{1}[a-z]{4}
    

    Every reply is appreciated

    1 Reply Last reply Reply Quote 1
    • T
      Terry R
      last edited by Terry R Dec 11, 2020, 6:53 AM Dec 11, 2020, 6:50 AM

      @Faisal-Alam said in Merge lines using Regex:

      I am able to find and select the text from my own RegEx but unable to merge it

      Firstly thanks very much for providing the examples within the boxes, especially the result you wish to get. You could have just added this post to your previous thread since it is the same subject matter.

      In that thread you said that @PeterJones solution did NOT work. From a test using his regex on the examples here I see that it DOES work. About the only thing I would add to his solution is that the last line needs to have an empty line afterwards as this regex expects that. Otherwise those last 2 lines in the file will not be combined.

      As for your regex, it doesn’t even select the 2 lines correctly as it is limited to 5 characters on the 2nd line of each set. Good on you for trying but you possibly need a bit more learning to understand what is required to correctly select the text or numbers.
      For example your ^\d.{3}, whilst it does collect the 4 digit number it will also grab a line such as 3abc. The more correct method would be to use ^\d+,.This grabs as many digits as exist on the line. If you want to be specific as to 4 digits only then \d{4}.
      And your ^[A-Z]{1}[a-z]{4} can be replaced with [a-z]{5}. The a-z also includes the A-Z unless there is a modifier at the start of the regex. Most times we don’t use the modifier to exclude either capital or small characters unless there is a real need to be exact. Nothing in your examples suggests that is necessary.

      So could you try @PeterJones regex on your data and if it doesn’t work can you provide some examples of what the problems are, currently I cannot see why it does NOT work. A possible issue might be that if you typed the examples here rather than copy and paste. If possible with your data in the Notepad++ tab, can you “show all characters”. This is done by selecting View, then Show Symbol, then Show All Characters. You will then see something like this:
      38db08b9-60f6-4823-aec2-f183292212cb-image.png
      Note the CR and LF which are equivalent to the \r and \n that you have in your regex.

      Thanks for coming back with the info above in the format we needed.

      Terry

      T F 2 Replies Last reply Dec 11, 2020, 7:29 AM Reply Quote 1
      • T
        Terry R
        last edited by Dec 11, 2020, 6:54 AM

        I should add that the image I posted was using your examples here with @PeterJones regex. Only addition was an empty line at the bottom before running the regex.

        Terry

        1 Reply Last reply Reply Quote 0
        • T
          Terry R @Terry R
          last edited by Dec 11, 2020, 7:29 AM

          @Terry-R said in Merge lines using Regex:

          About the only thing I would add to his solution is that the last line needs to have an empty line afterwards as this regex expects that. Otherwise those last 2 lines in the file will not be combined.

          That was wrong sorry (apologies to @PeterJones especially) I wasn’t paying much attention but thought it had missed the last set. Upon checking again I cannot see why it failed on that last set as now it does work.

          Terry

          1 Reply Last reply Reply Quote 1
          • F
            Faisal Alam @Terry R
            last edited by Dec 11, 2020, 7:55 AM

            @Terry-R brother i appreciate you so much for writing this long for an stranger like me but I might be unable to describe my problem.
            The problem is (the example i gave) isn’t the only thing in my txt file i have so many other lines which include numerous Numbers and Words, if you want that txt file then give me your mail I’ll send it. So my point is that @PeterJones RegEx definitely worked but not for my case, I can’t use that as it merges Numbers with Word in its next line. I hope you’re getting my point.
            I just want a regex which merges lines just like how \n works (it creates newline and i want something which merges its next line). Simple.

            Thanks anyways :)

            P 1 Reply Last reply Dec 11, 2020, 2:02 PM Reply Quote 1
            • T
              Terry R
              last edited by Dec 11, 2020, 8:03 AM

              @Faisal-Alam said in Merge lines using Regex:

              So my point is that @PeterJones RegEx definitely worked but not for my case, I can’t use that as it merges Numbers with Word in its next line

              Sorry but I do NOT understand the issue. The examples you provided should be representative of what you want to process. If there are other lines which you do NOT want to be changed you need to show them to us, otherwise we have no idea what needs to not be altered.

              As you had used the 4 digit option in your regex is it that ONLY 4 digit numbers should be added to the next line. If so then change @PeterJones regex to the following:
              Find What:(?-s)^(\d{4})\R+
              Leave the Replace Field as original.

              Terry

              1 Reply Last reply Reply Quote 1
              • P
                PeterJones @Faisal Alam
                last edited by Dec 11, 2020, 2:02 PM

                @Faisal-Alam said in Merge lines using Regex:

                I can’t use that as it merges Numbers with Word in its next line.

                The regex replacement I gave $1\x20 inserts a space between the number and the line that it merged with. It cannot merge the numbers with the word on the next line unless you skipped the \x20 and didn’t type the space in the replacement.

                As @Terry-R said, if your data is different from what you showed, you’re going to have to show something more representative of your data. You don’t have to give us the whole file. Just give us the smallest set of data that shows where the regex goes wrong.

                The answers you get will only be as good as the way you phrase the question and the data you supply.

                Good luck.

                P 1 Reply Last reply Dec 11, 2020, 2:16 PM Reply Quote 0
                • P
                  PeterJones @PeterJones
                  last edited by Dec 11, 2020, 2:16 PM

                  Once again, @Faisal-Alam moved the discussion from this topic to a third new topic for the same discussion. For future readers, sorry for the inconvenience.

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