[Regex] Remove all but last line



  • I often have to edit files that contain hundreds of lines (all urls begging with http or https) When I do this i only need the last url. It would speed my process if I could simply remove every line but the last one. Would love if this is possible, I’m not great with regex but have tried many things and have been googling for months with no luck. Would appreciate any help. Thank you



  • Hello, @bobby-teters222, and All,

    It’ not clear, Bobby, if you’re speaking about :

    • The last non-empty line of your files

    • The last line, beginning by the string http: or https:

    Anyway, here is the solutions for the both cases !


    • To delete all text till the last non-empty line, excluded, in any scanned file, use the regex S/R :

    SEARCH (?s).*\R(?=(?-s).+)

    REPLACE Leave EMPTY

    OPTIONS Wrap around and Regular expression ticked

    ACTION Click on the Replace All

    • To delete all text till the last line, beginning by http: or https:, excluded, in any scanned file, use the regex S/R :

    SEARCH (?s).*\R(?=(?-s)https?:.+)

    REPLACE Leave EMPTY

    OPTIONS Wrap around and Regular expression ticked

    ACTION Click on the Replace All


    If you like that solution, I’ll give you, next time, some details on these regexes !

    Best Regards,

    guy038



  • Thanks guy038 your second one worked perfectly for me. Really appreciate it.


Log in to reply