• Login
Community
  • Login

How to find numbers and start new lines with the numbers

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
9 Posts 4 Posters 3.9k 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
    marko marin
    last edited by marko marin May 23, 2018, 1:15 PM May 23, 2018, 1:14 PM

    Dear community,
    I know how to find numbers in text but I don’t know how to start new lines with those numbers. I want to start new lines with those numbers (which are increasing).
    The text is ;
    “1. “It isn’t so foggy today as it was yesterday” 2. “You can keep that book, if you like, Joan”,3. “Are you leaving today or tomorrow morning?” 4. “How far is it? And how long will it take me to get there?” 5. “Nothing grows in my garden. It never gets any sun”,”
    What I want is;
    “1. “It isn’t so foggy today as it was yesterday”
    2. “You can keep that book, if you like, Joan”,
    3. “Are you leaving today or tomorrow morning?”
    4. “How far is it? And how long will it take me to get there?”
    5. “Nothing grows in my garden. It never gets any sun”,”
    without losing the digital numbers at the beginning of the lines.
    Marin

    1 Reply Last reply Reply Quote 0
    • P
      PeterJones
      last edited by PeterJones May 23, 2018, 1:23 PM May 23, 2018, 1:22 PM

      • Find what : (.)(?=\d+\.)
      • Replace with : $1\n
      • ☑ Regular Expression

      Finds a single character followed by one or more digit characters followed by a period.
      Replaces with the single character, a newline, and leaves the original digit(s)+period intact.

      PS: see FAQ Desk: Where to find REGEX Documentation

      S 1 Reply Last reply May 23, 2018, 1:32 PM Reply Quote 3
      • S
        Scott Sumner @PeterJones
        last edited by May 23, 2018, 1:32 PM

        @PeterJones

        I would just make this slight tweak, as probably most users including the OP are using Windows and thus expect Windows line-endings:

        Replace with zone: $1\r\n

        1 Reply Last reply Reply Quote 3
        • M
          marko marin
          last edited by May 23, 2018, 1:40 PM

          It worked well for me
          Great thanks…

          1 Reply Last reply Reply Quote 1
          • P
            PeterJones
            last edited by May 23, 2018, 2:25 PM

            @Scott-Sumner,

            Thanks. Some of the programming languages I use are EOL-aware, so when I output \n, it actually does CRLF on Windows; thus, I sometimes forget in regex substitutions that \n is really just the LF portion (and since Notepad++ showed a new line between, and I didn’t have line-endings visible, when I tried the regex, I didn’t notice my mistake).

            1 Reply Last reply Reply Quote 4
            • M
              Markomarin
              last edited by Aug 6, 2018, 5:56 PM

              This solution created a problem with double digit numbers. The first digit and the second digit becomes separate in lines. What can I do to solve it?

              S 1 Reply Last reply Aug 6, 2018, 6:13 PM Reply Quote 0
              • S
                Scott Sumner @Markomarin
                last edited by Aug 6, 2018, 6:13 PM

                @Markomarin

                Maybe @PeterJones 's solution was a bit advanced for a regular expression noob. So maybe in the adjustment to handle multiple digits, it should also be simplified.

                I’d be looking at how to find one or more digits followed by a literal .…and then replace that match with a line-ending (\r\n on windows, \n on linux) followed by the original match text (hint on that last part: Use $0).

                Given that and your recent study of regular expressions, you should be able to take it to its completion.

                1 Reply Last reply Reply Quote 1
                • P
                  PeterJones
                  last edited by Aug 6, 2018, 7:14 PM

                  @Markomarin / @marko-marin ,

                  @Scott-Sumner is trying to help you to learn for yourself, and I applaud that effort on his part. My “PS: see FAQ Desk: Where to find REGEX Documentation” from three months ago will link you to some of the documentation that could help you research.

                  But as an alternative to his $0 hints (which may① be a better solution than mine), in the spirit of TIMTOWTD under which I learned my regexen: starting once again with my Find What : (.)(?=\d+\.) and Replace With : $1\r\n (as appropriately corrected by Scott earlier), by changing the first dot . in my original expression to a two-character escape sequence, I could parse

                  1. “It isn’t so foggy today as it was yesterday” 2. “You can keep that book, if you like, Joan”,3. “Are you leaving today or tomorrow morning?” 4. “How far is it? And how long will it take me to get there?” 5. “Nothing grows in my garden. It never gets any sun” 11. "Ours goes to Eleven" 12. "A dozen or more"
                  

                  to become

                  1. “It isn’t so foggy today as it was yesterday” 
                  2. “You can keep that book, if you like, Joan”,
                  3. “Are you leaving today or tomorrow morning?” 
                  4. “How far is it? And how long will it take me to get there?” 
                  5. “Nothing grows in my garden. It never gets any sun” 
                  11. "Ours goes to Eleven" 
                  12. "A dozen or more"
                  

                  … which is, I believe, what you now want.

                  As a further hint for what escape sequence to look for in the documentation: if the original dot . meant “find any character”, what might you want to do instead of finding any character. (Such as “find any character that’s not a flibberty-gibbert”, for some reasonable replacement for “flibberty-gibbert”)

                  ①: If using the sequence I believe he’s going for (which has one escape, one modifier, and one escape – with a total of five characters in the matching sequence), the first line may get a newline before it. Thus, for you, I would recommend going down both paths, and seeing which comes closer to your actual requirements.

                  But definitely: if you’re going to do more than the one regular expression you originally asked for, it will behoove you to study the documents, and practice, and start modifying it on your own, so you learn.

                  1 Reply Last reply Reply Quote 1
                  • P
                    PeterJones
                    last edited by Aug 6, 2018, 7:38 PM

                    (argh, typo. That should have been TIMTOWTDI… where’d my I go?)

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