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. 5.12 RegExpr Unicode Support
Qt 6.11 is out! See what's new in the release blog

5.12 RegExpr Unicode Support

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 705 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.
  • R Offline
    R Offline
    rhb327
    wrote on last edited by
    #1

    Does Qt 5.12 support the following?

    objRegExp =/^[\p{L}\p{N}-]{3,30}$/u
    return objRegExp.test(inputString)
    

    I can get this to work fine on https://regex101.com/ but I'm concerned ECMAScript versions might not be high enough in 5.12 for /p{...} support. If not, can someone describe a good method to localize a [a-zA-Z0-9] type regular expression?

    Thanks,
    -Rich

    JonBJ 1 Reply Last reply
    0
    • R rhb327

      Does Qt 5.12 support the following?

      objRegExp =/^[\p{L}\p{N}-]{3,30}$/u
      return objRegExp.test(inputString)
      

      I can get this to work fine on https://regex101.com/ but I'm concerned ECMAScript versions might not be high enough in 5.12 for /p{...} support. If not, can someone describe a good method to localize a [a-zA-Z0-9] type regular expression?

      Thanks,
      -Rich

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

      @rhb327
      I don't know the answer. But I recently discovered Qt comes with interactive QRegularExpression UI checker sample code at https://doc.qt.io/qt-5/qtwidgets-tools-regularexpression-example.html. Takes one minute to download & compile, useful from then onward for whatever version of Qt you are using.

      1 Reply Last reply
      2
      • R Offline
        R Offline
        rhb327
        wrote on last edited by
        #3

        Thanks! Link was helpful. I decided to move away from QML and I created a C++ support class kinda like this:

            qmlRegisterType<RegEx_Qml>("REQML", 1, 0, "RegEx_Qml");
            RegEx_Qml* reQML = new RegEx_Qml();
            engine->rootContext()->setContextProperty("reQML", reQML);
        
        #ifndef REGEX_QML_H
        #define REGEX_QML_H
        #include <QObject>
        #include <QRegularExpression>
        #include "constants.h"
        
        class RegEx_Qml : public QObject {
            Q_OBJECT
            Q_PROPERTY(QString valString READ valString WRITE setValString NOTIFY valStringChanged);
        
        public:
            explicit RegEx_Qml(QObject* parent = nullptr);
            ~RegEx_Qml();
            Q_INVOKABLE bool test(QString tst_str);
        
        signals:
            void valStringChanged(QString valString);
        
        private:
            QString valString() const { return m_valString; }
            void setValString(QString s) {
                m_valString = s;
                emit valStringChanged(m_valString);
            }
        
            QString m_valString;
            QRegularExpression regExp;
        };
        #endif // REGEX_QML_H
        
        
        RegEx_Qml::RegEx_Qml(QObject* parent) : QObject(parent) {
            regExp.setPatternOptions(QRegularExpression::UseUnicodePropertiesOption);
            setValString("");
        }
        
        RegEx_Qml::~RegEx_Qml() {
        }
        
        bool RegEx_Qml::test(QString tst_str) {
            regExp.setPattern(m_valString);
            QRegularExpressionMatch mtch = regExp.match(tst_str, 0);
        
        #ifdef VERBOSITY_HIGH
            qDebug() << "RegEx: " << tst_str << m_valString << regExp.match(tst_str, 0) << mtch.isValid() << mtch.hasMatch();
        #endif
        
            if(mtch.isValid()) {
                return mtch.hasMatch();
            }
            else {
                return false;
            }
        }
        

        QML Interface

                        RegEx_Qml {
                            id: regExQml
                        }
        
                        // validate Text fields with respect to strIndex
                        function validateInputData(inputIndex, inputString){
        
                            var objRegExp
                            switch(inputIndex) {
                            case 0 :
                                regExQml.valString = "^[\\w\\d.-]{4,20}$"
                                return regExQml.test(inputString)
                            case 1 :
                                console.log("Validating inputs for Pwd: ", usermodel_AddUser.get(0).labelText,usermodel_AddUser.count)
                                if(usermodel_AddUser.get(0).labelText==="Test" || usermodel_AddUser.get(0).labelText==="service"){
                                    regExQml.valString = "^[\\w\\d.-]{4,20}$"
                                    return regExQml.test(inputString)
                                }
        
        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