QRegExp replece issue



  • Hi,

    I want separate the following string by "-" but unfortunately QRegExp doesn't match it

    rg

    (.*) - (.....) (.*)
    

    replace

    \1 - \2 - \3
    

    string

    KÜÇÜK OTOBÜS TAYO - S01E12 GEL ARKADAŞ OLALIM
    

    I expect to get

    KÜÇÜK OTOBÜS TAYO - S01E12 - GEL ARKADAŞ OLALIM
    


  • Hi @mbnoimi,

    The problem is that you don't have enough dots in the inner group - you have 5 dots, which matches 5 characters, but the input has 6 characters S01E12.

    # Doesn't work...
    paul@paul-XPS-13-9343:~$ echo 'KÜÇÜK OTOBÜS TAYO - S01E12 GEL ARKADAŞ OLALIM' | sed -re 's/(.*) - (.....) (.*)/\1 - \2 - \3/'
    KÜÇÜK OTOBÜS TAYO - S01E12 GEL ARKADAŞ OLALIM
    # Add another '.' and all is good :)
    paul@paul-XPS-13-9343:~$ echo 'KÜÇÜK OTOBÜS TAYO - S01E12 GEL ARKADAŞ OLALIM' | sed -re 's/(.*) - (......) (.*)/\1 - \2 - \3/'
    KÜÇÜK OTOBÜS TAYO - S01E12 - GEL ARKADAŞ OLALIM
    

    Cheers.



  • @Paul-Colby said in QRegExp replece issue:

    The problem is that you don't have enough dots in the inner group - you have 5 dots, which matches 5 characters, but the input has 6 characters S01E12.

    Indeed! Thanks



  • side notes:

    • if you are using Qt5, do not use QRegExp, use QRegularExpression
    • .{6} is safer and faster than ......
    • be careful with the greediness of * operators here


  • @VRonin Thanks for advice,


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.