Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QRegularExpression, implying regex on data contains NULL character
Forum Updated to NodeBB v4.3 + New Features

QRegularExpression, implying regex on data contains NULL character

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 351 Views 1 Watching
  • 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.
  • I Offline
    I Offline
    ikuris
    wrote on last edited by ikuris
    #1

    Hello,
    I am trying to parse some inputs that can have multiple NULL characters in it but I couldnt make it word, here is code I tried.

        QByteArray data = "[12]Hello\0\0World";
        QRegularExpression reg("(?<h1>\\[.*\\])(?<h2>.*)");
        reg.setPatternOptions(QRegularExpression::DotMatchesEverythingOption);
        QRegularExpressionMatchIterator itr = reg.globalMatch(data);
        while(itr.hasNext())
        {
            QRegularExpressionMatch match = itr.next();
            qDebug() << match.captured("h2");
        }
    

    I want it to print HelloWorld but all I can see is Hello,
    Thank you.

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      It does not even compile. Since a QString should not contain a \0 (and you don't show how you convert your data to str) I doubt this will work at all with a regexp. Looks like another problem which shows a use of a regex where a simple search and replace would be much more efficent and understandable.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • I Offline
        I Offline
        ikuris
        wrote on last edited by
        #3

        Sorry, I was trying with QString str = QString::fromLatin1(data, data.size). It didnot work either.
        I updated the code.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          That's one of the issue: a string can only have one terminator char. Work on your QByteArray to change them. For example use the replace function.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • I ikuris

            Hello,
            I am trying to parse some inputs that can have multiple NULL characters in it but I couldnt make it word, here is code I tried.

                QByteArray data = "[12]Hello\0\0World";
                QRegularExpression reg("(?<h1>\\[.*\\])(?<h2>.*)");
                reg.setPatternOptions(QRegularExpression::DotMatchesEverythingOption);
                QRegularExpressionMatchIterator itr = reg.globalMatch(data);
                while(itr.hasNext())
                {
                    QRegularExpressionMatch match = itr.next();
                    qDebug() << match.captured("h2");
                }
            

            I want it to print HelloWorld but all I can see is Hello,
            Thank you.

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

            @ikuris said in QRegularExpression, implying regex on data contains NULL character:

            QRegularExpression reg("(?<h1>\\[.*\\])(?<h2>.*)");
            

            Separately from the correct observations of my colleagues. What are \\[ and \\] doing here inside a C literal? What are you trying to achieve with these?

            1 Reply Last reply
            0

            • Login

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