Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved QRegExp replece issue

    General and Desktop
    3
    5
    817
    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
      mbnoimi last edited by

      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
      
      Paul Colby 1 Reply Last reply Reply Quote 0
      • Paul Colby
        Paul Colby @mbnoimi last edited by

        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.

        M 1 Reply Last reply Reply Quote 2
        • M
          mbnoimi @Paul Colby last edited by

          @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

          1 Reply Last reply Reply Quote 0
          • VRonin
            VRonin last edited by

            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

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            M 1 Reply Last reply Reply Quote 2
            • M
              mbnoimi @VRonin last edited by

              @VRonin Thanks for advice,

              1 Reply Last reply Reply Quote 0
              • First post
                Last post