How to find numbers and start new lines with the numbers



  • 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



    • 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



  • @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



  • It worked well for me
    Great thanks…



  • @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).



  • 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?



  • @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.



  • @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.



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


Log in to reply