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. How to match each number within alphanumeric string.
Forum Updated to NodeBB v4.3 + New Features

How to match each number within alphanumeric string.

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 4 Posters 1.2k Views 2 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.
  • ademmlerA ademmler

    Another wired situation in the same project:
    I am looping a text file where each line contains a color and some color data.
    I am looking for line where the Beginning matches a color name like this:

    QString  searchString = "COLOR BLUE 072 C";
    
    while (!file.atEnd()) {        
            QString line = file.readLine();
            QStringList list = line.split(QRegExp("(\\s+)|(\\t)"));
    
     String color = list[0];
            color.replace(QRegularExpression("\\^"), " ");
            color.replace(QRegularExpression("\\t"), "");
    
            QRegularExpression rx(searchString);
            QRegularExpressionMatch match = rx.match(color, Qt::MatchRegExp);
    
    if(match.hasMatch() { do some thing ...
    

    match.hasMatch() is always "false" even if the searchString and color are identical (checked multiple times).

    I also tried Qt::MatchExactly and Qt::MatchContains without success.

    In oposite "list[0].contains(color)" returns "true".

    What I am missing here.

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

    @ademmler

    QRegularExpressionMatch match = rx.match(color, Qt::MatchRegExp);
    

    Can you please do us and yourself a favor a print out what rx and color are when it fails?

    QRegularExpression rx(searchString);    // searchString = "COLOR BLUE 072 C"
    

    does not look like much of a regular expression to me....

    On a separate matter:

    QStringList list = line.split(QRegExp("(\\s+)|(\\t)"));
    

    What version of Qt are you using? Using QRegExp, and mixing it with QRegularExpression, is not a good idea. Additionally, what is the point/aim of using \s*|\t as a (splitting) regular expression? I don't get it.

    QRegularExpressionMatch match = rx.match(color, Qt::MatchRegExp);
    

    Again, I don't know what version of Qt you are using (Qt5??) but what overload of QRegularExpression::match() are you using which offers Qt::MatchRegExp as a value for what type?

    ademmlerA 1 Reply Last reply
    2
    • JonBJ JonB

      @ademmler

      QRegularExpressionMatch match = rx.match(color, Qt::MatchRegExp);
      

      Can you please do us and yourself a favor a print out what rx and color are when it fails?

      QRegularExpression rx(searchString);    // searchString = "COLOR BLUE 072 C"
      

      does not look like much of a regular expression to me....

      On a separate matter:

      QStringList list = line.split(QRegExp("(\\s+)|(\\t)"));
      

      What version of Qt are you using? Using QRegExp, and mixing it with QRegularExpression, is not a good idea. Additionally, what is the point/aim of using \s*|\t as a (splitting) regular expression? I don't get it.

      QRegularExpressionMatch match = rx.match(color, Qt::MatchRegExp);
      

      Again, I don't know what version of Qt you are using (Qt5??) but what overload of QRegularExpression::match() are you using which offers Qt::MatchRegExp as a value for what type?

      ademmlerA Offline
      ademmlerA Offline
      ademmler
      wrote on last edited by
      #11

      @JonB said in How to match each number within alphanumeric string.:

      Dear John - thx for helping me. It took some time to come back here.
      Let me try to answer your questions.

      On a separate matter:
      QStringList list = line.split(QRegExp("(\s+)|(\t)"));

      What version of Qt are you using?

      Version 5.15.13

      Using QRegExp, and mixing it with QRegularExpression, is not a good idea.

      OK - how to do better?

      Additionally, what is the point/aim of using \s*|\t as a (splitting) regular >expression? I don't get it.

      A line could look like this. Between the fields there can be one or multiple blank or tab. I want to get the filed values only - without tabs and spaces.

      BLACK^BL PAPER_100 25.9815 0.287622 -1.5913 0.036 0.04 0.0453 0.0507 0.0535 0.0521 0.0503 0.0501 0.0508 0.0507 0.0493 0.0484 0.0479 0.0478 0.0481 0.0475 0.0464 0.0456 0.0456 0.0463 0.047 0.0475 0.0482 0.0482 0.048 0.0484 0.0488 0.0488 0.0489 0.0497 0.0493

      QRegularExpression rx(searchString); // searchString = "COLOR BLUE 072 C"
      does not look like much of a regular expression to me....

      Because this changes for every search, I want to pass (from somewhere else) the "search phrase/string" as variable. In this case it should match 100% the string. I thought it is the same as writing: QRegularExpression rx("COLOR BLUE 072 C");

      I am very open to learn and improve - you may tell me how to do better.

      JonBJ 1 Reply Last reply
      0
      • ademmlerA ademmler

        @JonB said in How to match each number within alphanumeric string.:

        Dear John - thx for helping me. It took some time to come back here.
        Let me try to answer your questions.

        On a separate matter:
        QStringList list = line.split(QRegExp("(\s+)|(\t)"));

        What version of Qt are you using?

        Version 5.15.13

        Using QRegExp, and mixing it with QRegularExpression, is not a good idea.

        OK - how to do better?

        Additionally, what is the point/aim of using \s*|\t as a (splitting) regular >expression? I don't get it.

        A line could look like this. Between the fields there can be one or multiple blank or tab. I want to get the filed values only - without tabs and spaces.

        BLACK^BL PAPER_100 25.9815 0.287622 -1.5913 0.036 0.04 0.0453 0.0507 0.0535 0.0521 0.0503 0.0501 0.0508 0.0507 0.0493 0.0484 0.0479 0.0478 0.0481 0.0475 0.0464 0.0456 0.0456 0.0463 0.047 0.0475 0.0482 0.0482 0.048 0.0484 0.0488 0.0488 0.0489 0.0497 0.0493

        QRegularExpression rx(searchString); // searchString = "COLOR BLUE 072 C"
        does not look like much of a regular expression to me....

        Because this changes for every search, I want to pass (from somewhere else) the "search phrase/string" as variable. In this case it should match 100% the string. I thought it is the same as writing: QRegularExpression rx("COLOR BLUE 072 C");

        I am very open to learn and improve - you may tell me how to do better.

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

        @ademmler said in How to match each number within alphanumeric string.:

        Using QRegExp, and mixing it with QRegularExpression, is not a good idea.

        OK - how to do better?

        Do not #include <QRegExp> and do not use QRegExp. Use only QRegularExpression.

        I thought it is the same as writing: QRegularExpression rx("COLOR BLUE 072 C");

        I am not clear what you think you are doing, and I am not sure you are either! If you have a regular expression of COLOR BLUE 072 C how can that possibly match input anything like BLACK^BL PAPER_100 25.9815 0.287622 ..., and how could it split on whitespace and/or capture anything? I wonder whether you mean this is the input string to be matched by the regular expression...??

        If all you want to do is split a string on whitespace, and receive a list of the items in between, all you need is QStringList QString::split(const QRegularExpression &re, Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const. The example there gives

        str = "Some  text\n\twith  strange whitespace.";
        list = str.split(QRegularExpression("\\s+"));
        // list: [ "Some", "text", "with", "strange", "whitespace." ]
        
        ademmlerA 2 Replies Last reply
        2
        • JonBJ JonB

          @ademmler said in How to match each number within alphanumeric string.:

          Using QRegExp, and mixing it with QRegularExpression, is not a good idea.

          OK - how to do better?

          Do not #include <QRegExp> and do not use QRegExp. Use only QRegularExpression.

          I thought it is the same as writing: QRegularExpression rx("COLOR BLUE 072 C");

          I am not clear what you think you are doing, and I am not sure you are either! If you have a regular expression of COLOR BLUE 072 C how can that possibly match input anything like BLACK^BL PAPER_100 25.9815 0.287622 ..., and how could it split on whitespace and/or capture anything? I wonder whether you mean this is the input string to be matched by the regular expression...??

          If all you want to do is split a string on whitespace, and receive a list of the items in between, all you need is QStringList QString::split(const QRegularExpression &re, Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const. The example there gives

          str = "Some  text\n\twith  strange whitespace.";
          list = str.split(QRegularExpression("\\s+"));
          // list: [ "Some", "text", "with", "strange", "whitespace." ]
          
          ademmlerA Offline
          ademmlerA Offline
          ademmler
          wrote on last edited by
          #13

          @JonB said in How to match each number within alphanumeric string.:

          I am not clear what you think you are doing, and I am not sure you are either! If you have a regular expression of COLOR BLUE 072 C how can that possibly match input anything like BLACK^BL PAPER_100 25.9815 0.287622 ..., and how could it split on whitespace and/or capture anything? I wonder whether you mean this is the input string to be matched by the regular expression...??

          Dear John, sorry for confusing you. in my file is 1000 of lines. Each represents a color. My example was of course not matching the search string.
          It was just a sample for "how the lines is structured".

          Somewhere in this file should be a line like this - wich is the one I am searching for.
          COLOR^BLUE^072^C PAPER_100 25.9815 0.287622 ...
          or it is
          BLUE^072^C PAPER_100 25.9815 0.287622 ...
          or
          COLOR^072 PAPER_100 25.9815 0.287622 ...

          And all I need to catch ...

          1 Reply Last reply
          0
          • JonBJ JonB

            @ademmler said in How to match each number within alphanumeric string.:

            Using QRegExp, and mixing it with QRegularExpression, is not a good idea.

            OK - how to do better?

            Do not #include <QRegExp> and do not use QRegExp. Use only QRegularExpression.

            I thought it is the same as writing: QRegularExpression rx("COLOR BLUE 072 C");

            I am not clear what you think you are doing, and I am not sure you are either! If you have a regular expression of COLOR BLUE 072 C how can that possibly match input anything like BLACK^BL PAPER_100 25.9815 0.287622 ..., and how could it split on whitespace and/or capture anything? I wonder whether you mean this is the input string to be matched by the regular expression...??

            If all you want to do is split a string on whitespace, and receive a list of the items in between, all you need is QStringList QString::split(const QRegularExpression &re, Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const. The example there gives

            str = "Some  text\n\twith  strange whitespace.";
            list = str.split(QRegularExpression("\\s+"));
            // list: [ "Some", "text", "with", "strange", "whitespace." ]
            
            ademmlerA Offline
            ademmlerA Offline
            ademmler
            wrote on last edited by
            #14

            @JonB said in How to match each number within alphanumeric string.:

            Do not #include <QRegExp> and do not use QRegExp. Use only QRegularExpression.

            Interesting I have searched the whole project - and I do not do #include <QRegExp> anywhere. How comes that "QRegExp" is working than ...

            JonBJ 1 Reply Last reply
            0
            • ademmlerA ademmler

              @JonB said in How to match each number within alphanumeric string.:

              Do not #include <QRegExp> and do not use QRegExp. Use only QRegularExpression.

              Interesting I have searched the whole project - and I do not do #include <QRegExp> anywhere. How comes that "QRegExp" is working than ...

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

              @ademmler
              #include <QString> is probably enough to call line.split(QRegExp(...)). That does not even exist in Qt6. It may do at Qt5. But that still has QString::split(const QRegularExpression &re, ...), use that instead.

              For the 3 lines you show I don't know what it is that you want to search for "commonality". I'm afraid your requirements/rules are not expressed clearly enough. Each of the 3 starts with something different. You need to stipulate precisely what you want to search for/exclude in order to design the right code or regular expression. Or you could just do the searching on the elements returned by str.split(QRegularExpression("\\s+")) without worrying about a complex regular expression to match the line.

              ademmlerA 1 Reply Last reply
              0
              • JonBJ JonB

                @ademmler
                #include <QString> is probably enough to call line.split(QRegExp(...)). That does not even exist in Qt6. It may do at Qt5. But that still has QString::split(const QRegularExpression &re, ...), use that instead.

                For the 3 lines you show I don't know what it is that you want to search for "commonality". I'm afraid your requirements/rules are not expressed clearly enough. Each of the 3 starts with something different. You need to stipulate precisely what you want to search for/exclude in order to design the right code or regular expression. Or you could just do the searching on the elements returned by str.split(QRegularExpression("\\s+")) without worrying about a complex regular expression to match the line.

                ademmlerA Offline
                ademmlerA Offline
                ademmler
                wrote on last edited by ademmler
                #16

                @JonB said in How to match each number within alphanumeric string.:

                For the 3 lines you show I don't know what it is that you want to search for "commonality". I'm afraid your requirements/rules are not expressed clearly enough. Each of the 3 starts with something different. You need to stipulate precisely what you want to search for/exclude in order to design the right code or regular expression. Or you could just do the searching on the elements returned by str.split(QRegularExpression("\s+")) without worrying about a complex regular expression to match the line.

                I had the same thought - I think it needs a loop going through the elements of the search string and loop through the elements of the "first line element" ... if one matches here we go.

                I try to explain you the problem behind - it is coming form printing workflow.
                A user creates a PDF using his custom color names for color separations:
                Example: A file with two colors "Monkey Blue" and "Turtle Red 132"

                At the print plant they use other color names - for the same color:
                Like "PT Blue" and "Color 132". In production those needs to be matched:
                PT Blue = Monkey Blue
                Turtle Red 132 = Color 132

                For the sake of illustration i used this simple color names.
                Of course real life those are more complex to match ...

                JonBJ 1 Reply Last reply
                0
                • ademmlerA ademmler

                  @JonB said in How to match each number within alphanumeric string.:

                  For the 3 lines you show I don't know what it is that you want to search for "commonality". I'm afraid your requirements/rules are not expressed clearly enough. Each of the 3 starts with something different. You need to stipulate precisely what you want to search for/exclude in order to design the right code or regular expression. Or you could just do the searching on the elements returned by str.split(QRegularExpression("\s+")) without worrying about a complex regular expression to match the line.

                  I had the same thought - I think it needs a loop going through the elements of the search string and loop through the elements of the "first line element" ... if one matches here we go.

                  I try to explain you the problem behind - it is coming form printing workflow.
                  A user creates a PDF using his custom color names for color separations:
                  Example: A file with two colors "Monkey Blue" and "Turtle Red 132"

                  At the print plant they use other color names - for the same color:
                  Like "PT Blue" and "Color 132". In production those needs to be matched:
                  PT Blue = Monkey Blue
                  Turtle Red 132 = Color 132

                  For the sake of illustration i used this simple color names.
                  Of course real life those are more complex to match ...

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

                  @ademmler
                  Sorry, but I don't think this explanation has anything to say about why the 3 lines you give are of interest and others are not. Your 3 lines seem to be identical to the right; to the left they start with any of COLOR^BLUE, BLUE and/or COLOR. Come back when you have stipulated whatever rules you want to impose.

                  If you have a lot of situations where you wish to "map" one string to another it might be easier not to try to do it with (lots of?) regular expression matches. Rather just read the strings and use QMap to set up mappings which you look up.

                  ademmlerA 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @ademmler
                    Sorry, but I don't think this explanation has anything to say about why the 3 lines you give are of interest and others are not. Your 3 lines seem to be identical to the right; to the left they start with any of COLOR^BLUE, BLUE and/or COLOR. Come back when you have stipulated whatever rules you want to impose.

                    If you have a lot of situations where you wish to "map" one string to another it might be easier not to try to do it with (lots of?) regular expression matches. Rather just read the strings and use QMap to set up mappings which you look up.

                    ademmlerA Offline
                    ademmlerA Offline
                    ademmler
                    wrote on last edited by
                    #18

                    @JonB Dear John,

                    Come back when you have stipulated whatever rules you want to impose.

                    There is no rule! That is the problem to solve. With a rule it would be easy.

                    Using QMap is not possible too - because in my case there is millions of unknown combinations. This means in any location different target values and from any print job other input values ... hence I try to develop a "guess it right" logic.

                    Don't worry - I will find a way. Thx for your valuable hints.

                    JonBJ 1 Reply Last reply
                    0
                    • ademmlerA ademmler

                      @JonB Dear John,

                      Come back when you have stipulated whatever rules you want to impose.

                      There is no rule! That is the problem to solve. With a rule it would be easy.

                      Using QMap is not possible too - because in my case there is millions of unknown combinations. This means in any location different target values and from any print job other input values ... hence I try to develop a "guess it right" logic.

                      Don't worry - I will find a way. Thx for your valuable hints.

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

                      @ademmler said in How to match each number within alphanumeric string.:

                      There is no rule! That is the problem to solve. With a rule it would be easy.

                      Sorry, this makes no sense. If you have no "rule" for what you are looking for how can you possibly know what it is you want or what code to right?

                      If you show 3 lines you want to recognise, and not other lines, of course there is some rule for what you want to pick out.... It might be a "best guess" but you still have to have something you wish to implement for this.

                      ademmlerA 1 Reply Last reply
                      2
                      • JonBJ JonB

                        @ademmler said in How to match each number within alphanumeric string.:

                        There is no rule! That is the problem to solve. With a rule it would be easy.

                        Sorry, this makes no sense. If you have no "rule" for what you are looking for how can you possibly know what it is you want or what code to right?

                        If you show 3 lines you want to recognise, and not other lines, of course there is some rule for what you want to pick out.... It might be a "best guess" but you still have to have something you wish to implement for this.

                        ademmlerA Offline
                        ademmlerA Offline
                        ademmler
                        wrote on last edited by
                        #20

                        @JonB Are you in printing business or have experience? If you like let us have a Skype chat on this - might be easier.

                        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