Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

5.12 RegExpr Unicode Support



  • 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



  • @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.



  • 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)
                            }
    

Log in to reply