[Python] Surrounding whitespace-separated URLs with quotes using sed

Discussão em 'Python' iniciado por Stack, Maio 3, 2025.

  1. Stack

    Stack Membro Participativo

    Problem


    I was trying to get sed command to do the same thing I could do with Python regex flavour, but I encountered some problems

    Python regex example: (tested it on regex101 and it was working fine)

    find: (https.*?)

    replace: "\1"

    Unsuccessful code:

    sed 's/\(https.*?\)[:space:]/\"\1\"/g' .\elenco.txt


    elenco.txt file:

    https://www.youtube.com/watch?app=desktop&v=Ot34P0yyQqI&t=984s https://www.youtube.com/watch?v=vviniZjvDQs https://www.youtube.com/watch?v=Ih7qgkyo_oo https://www.youtube.com/watch?v=X6UEDpwI3HI https://www.youtube.com/watch?v=nShgaRMNlLw https://www.youtube.com/watch?v=nd_jN-C_Juw https://www.youtube.com/watch?v=aOtqox2uB3Y


    Expected output:

    "https://www.youtube.com/watch?app=desktop&v=Ot34P0yyQqI&t=984s" "https://www.youtube.com/watch?v=vviniZjvDQs" "https://www.youtube.com/watch?v=Ih7qgkyo_oo" "https://www.youtube.com/watch?v=X6UEDpwI3HI" "https://www.youtube.com/watch?v=nShgaRMNlLw" "https://www.youtube.com/watch?v=nd_jN-C_Juw" "https://www.youtube.com/watch?v=aOtqox2uB3Y"


    Actual output:

    "https://www.youtube.com/watch?"pp=desktop&v=Ot34P0yyQqI&t=984s https://www.youtube.com/watch?v=vviniZjvDQs https://www.youtube.com/watch?v=Ih7qgkyo_oo https://www.youtube.com/watch?v=X6UEDpwI3HI https://www.youtube.com/watch?v=nShgaRMNlLw https://www.youtube.com/watch?v=nd_jN-C_Juw https://www.youtube.com/watch?v=aOtqox2uB3Y

    Info

    OS


    Name: Microsoft Windows 11 Home Version: 10.0.26100 N/D build 26100

    installed sed through winget install bmatzelle.Gow

    I've always avoided using POSIX regex etc, as I found it unnecessarily complicated / limited compared to using perl/python etc. and the regex flavour available there.

    Any other options than to install Perl/Python? 200MB for StrawberryPerl (Perl on Windows) seems to be quite overkill and useless bloat just to have access to perl flavour regex, and sed unlike perl doen't support 'easy' regex...

    https://askubuntu.com/questions/1050693/sed-with-pcre-like-grep-p

    Continue reading...

Compartilhe esta Página