Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to pass regular expresion to QString.contains (reg expression) ??
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved C++ Gurus
2 Posts 2 Posters 332 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 29 Oct 2023, 16:21 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);
    
    
    
    
    
    
    
    J 1 Reply Last reply 30 Oct 2023, 12:01
    0
    • A Anonymous_Banned275
      29 Oct 2023, 16:21

      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);
      
      
      
      
      
      
      
      J Offline
      J Offline
      JonB
      wrote on 30 Oct 2023, 12:01 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

      2/2

      30 Oct 2023, 12:01

      • Login

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