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. Matching a regular expression in string.
QtWS25 Last Chance

Matching a regular expression in string.

Scheduled Pinned Locked Moved General and Desktop
qregexpqstring
4 Posts 2 Posters 10.3k 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.
  • NIXINN Offline
    NIXINN Offline
    NIXIN
    wrote on last edited by NIXIN
    #1

    I am trying something like below,

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    QString str = "((Hello1 and Hello2) in Hello.World)";

    QString matchWord = "Hello;

    qDebug() << str.contains(QRegExp("\\b" + matchWord + "\\b"));

    return a.exec();
    }

    I am getting output as true. I want to what's wrong??

    My intention is to match only a complete word, like "Hello".

    Can anyone help me with this?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      I think the engine matches the "Hello.World", treating the dot (.) as a word boundary.

      You can check where the match happened using QRegExp API to be sure.

      BTW. It is recommended to use QRegularExpression in Qt5 - QRegExp is just a leftover from Qt4 times.

      (Z(:^

      1 Reply Last reply
      2
      • NIXINN Offline
        NIXINN Offline
        NIXIN
        wrote on last edited by
        #3

        can you provide a small piece of code

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Sure, they are in the docs: http://doc.qt.io/qt-5/qregexp.html#code-examples

          indexIn() method should interest you in this case.

          (Z(:^

          1 Reply Last reply
          3

          • Login

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