Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to pass regular expresion to QString.contains (reg expression) ??
Qt 6.12 LTS is out! See what's new in the release blog

How to pass regular expresion to QString.contains (reg expression) ??

Scheduled Pinned Locked Moved Unsolved C++ Gurus
2 Posts 2 Posters 867 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.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on last edited by
    #1

    I am testing /learning how to use regular expression.
    I like this code to match all ASCII characters as words .

    When used like this ,
    QRegularExpression re("[ -~]+");
    it correctly matches first words in the inString

    However when used in .contains it fails.
    I have included two attempts to implement .contains with similar parameter passed.

    I am asking for help to write passed regular expression correctly.
    My syntax pass by compiler but does not work.

    Please - no references to AI reg expression generators - they do not work when tested expression code is passed to Qt.

    THANKS

            if(inString.contains(^[ -~]+$ ))
            {
                text = "Match ";
            }else
            {
                text = " No match ";
            }
    
            qDebug() << text;
            textDEBUG->append(text);
    
    OR 
    
          if(inString.contains("[ -~]+"))
            {
                text = "Match ";
            }else
            {
                text = " No match ";
            }
    
            qDebug() << text;
            textDEBUG->append(text);
    
    
    
    
    
    
    
    JonBJ 1 Reply Last reply
    0
    • A Anonymous_Banned275

      I am testing /learning how to use regular expression.
      I like this code to match all ASCII characters as words .

      When used like this ,
      QRegularExpression re("[ -~]+");
      it correctly matches first words in the inString

      However when used in .contains it fails.
      I have included two attempts to implement .contains with similar parameter passed.

      I am asking for help to write passed regular expression correctly.
      My syntax pass by compiler but does not work.

      Please - no references to AI reg expression generators - they do not work when tested expression code is passed to Qt.

      THANKS

              if(inString.contains(^[ -~]+$ ))
              {
                  text = "Match ";
              }else
              {
                  text = " No match ";
              }
      
              qDebug() << text;
              textDEBUG->append(text);
      
      OR 
      
            if(inString.contains("[ -~]+"))
              {
                  text = "Match ";
              }else
              {
                  text = " No match ";
              }
      
              qDebug() << text;
              textDEBUG->append(text);
      
      
      
      
      
      
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @AnneRanch

      if(inString.contains(^[ -~]+$ ))
      

      This will not compile as you are not passing it either a QString or a QRegularExpression. You will get a syntax error.

      if(inString.contains("[ -~]+"))
      

      This will compile. But because you don't pass any regular expression, only a plain string, it will look to see whether that literal string appears in your test string, which it won't.

      You meant:

      if(inString.contains(QRegularExpression("[ -~]+")))
      

      which will tell you whether your string contains one or more ASCII characters.

      That would be true if your string contains a mixture of ASCII and non-ASCII characters. If you want to "fail" on any non-ASCII characters (i.e. only succeed on all ASCII characters) you want something like:

      if (QRegularExpression("^[ -~]+$").match(inString).hasMatch())
      
      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