How to match each number within alphanumeric string.
-
QRegularExpressionMatch match = rx.match(color, Qt::MatchRegExp);
Can you please do us and yourself a favor a print out what
rx
andcolor
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 withQRegularExpression
, 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 offersQt::MatchRegExp
as a value for what type? -
@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.
-
@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 useQRegExp
. Use onlyQRegularExpression
.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 likeBLACK^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." ]
-
@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 ...
-
@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 ...
-
@ademmler
#include <QString>
is probably enough to callline.split(QRegExp(...))
. That does not even exist in Qt6. It may do at Qt5. But that still hasQString::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. -
@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 132For the sake of illustration i used this simple color names.
Of course real life those are more complex to match ... -
@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 ofCOLOR^BLUE
,BLUE
and/orCOLOR
. 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. -
@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.
-
@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.