Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. RegexReplace
Qt 6.12 LTS is out! See what's new in the release blog

RegexReplace

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.5k Views
  • 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.
  • CesarC Offline
    CesarC Offline
    Cesar
    wrote on last edited by Cesar
    #1
    QString QRegexReplace(coQString str, const QString& regex
        , const QString& replace, QRegularExpression::PatternOption flag)
    {
        QRegularExpression re(regex, flag);
        return str.replace(re, replace);
    }
    
    QString s = "111111111111111111111111111111111111111110";
     s = QRegexReplace(s, ".{6}", "|$0");
    

    My function is replacing s to |$0|$0|$0|$0|$0|$0|$0

    How i could get the same result as seen on: https://regex101.com/r/bsgCKf/1
    Im trying to get s to |111111|111111|111111|111111|111111|111111|111110

    JonBJ 1 Reply Last reply
    0
    • CesarC Cesar
      QString QRegexReplace(coQString str, const QString& regex
          , const QString& replace, QRegularExpression::PatternOption flag)
      {
          QRegularExpression re(regex, flag);
          return str.replace(re, replace);
      }
      
      QString s = "111111111111111111111111111111111111111110";
       s = QRegexReplace(s, ".{6}", "|$0");
      

      My function is replacing s to |$0|$0|$0|$0|$0|$0|$0

      How i could get the same result as seen on: https://regex101.com/r/bsgCKf/1
      Im trying to get s to |111111|111111|111111|111111|111111|111111|111110

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Cesar
      Good question! I cannot test for Qt, but try

      s = QRegexReplace(s, "(.{6})", "|\\1");
      

      ?

      https://regex101.com/r/YOrWq9/1

      1 Reply Last reply
      2
      • CesarC Offline
        CesarC Offline
        Cesar
        wrote on last edited by
        #3

        This does work, but i would like to understand why mregex isn't working?

        JonBJ 1 Reply Last reply
        0
        • CesarC Cesar

          This does work, but i would like to understand why mregex isn't working?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Cesar
          I don't know, your way is not supported by the regular expressions used in Qt is all I can say. The one I gave you works in PCRE (PHP < 7.3), maybe that is what Qt uses.

          D 1 Reply Last reply
          0
          • JonBJ JonB

            @Cesar
            I don't know, your way is not supported by the regular expressions used in Qt is all I can say. The one I gave you works in PCRE (PHP < 7.3), maybe that is what Qt uses.

            D Offline
            D Offline
            DerReisende
            wrote on last edited by
            #5

            @JonB
            From the Qt5/Qt6 doc:
            „QRegularExpression implements Perl-compatible regular expressions.“

            JonBJ 1 Reply Last reply
            0
            • D DerReisende

              @JonB
              From the Qt5/Qt6 doc:
              „QRegularExpression implements Perl-compatible regular expressions.“

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @DerReisende
              The problem is that is apparently/evidently not true, or not sufficiently accurate!

              On the regex site they offer "flavor" of PCRE2 (PHP >= 7.3) and their "Help" for that states "Perl PCRE2". And if you use |$0 for Substitution that gives the OP's desired result of |111111|111111|111111|111111|111111|111111|111110. You can see this from the link the OP supplied: https://regex101.com/r/bsgCKf/1

              That would be fine if OP then reported using that in Qt as QRegexReplace(s, ".{6}", "|$0"); delivered same. The problem is it does not! He says that gives him |$0|$0|$0|$0|$0|$0|$0, i.e. $0 is not being recognised/substituted. That can be seen if e.g. you change "flavor" over to Python, which does not recognise $0.

              I found that did work in various "flavors" with parentheses-capture and \1, see my https://regex101.com/r/YOrWq9/1, hence my suggestion OP try QRegexReplace(s, "(.{6})", "|\\1"); in Qt, which he confirms "This does work"....

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt
              • Unsolved