
Originally Posted by
jesse_mark
line = "V1= V2=x V3 = y"
if one of the variables were missing its value, the both ways gave me wrong result.

Why of course, Sir, here's what you specified:
string {any number of spaces} = String {any unmber of spaces} String = string)
with no mention of the possibility of empty strings.
With your new input how is the expression supposed to know that V2 is the name of a second variable and not the value of the first? You might say, "but it's followed by an equal sign, and that makes it a name" That is not an adequate description because you allow space between the name and the '=' e.g. if the line was:
V1= V2 = x V3 = y
V1= V2 = x V3 = y
To copy to clipboard, switch view to plain text mode
A naïve attempt using my base regexp:
QString line
= "v1= v2 = value2 v3= value3";
QRegExp re
("\\w+\\s*=\\s*\\w*(?!\\s+=)");
// ("v1= v", "2 = value2", "v3= value3")
QString line = "v1= v2 = value2 v3= value3";
QRegExp re("\\w+\\s*=\\s*\\w*(?!\\s+=)");
// ("v1= v", "2 = value2", "v3= value3")
To copy to clipboard, switch view to plain text mode
fails in new and unexpected ways. This comes closer:
QRegExp re
("(\\w+\\s*=\\s*\\w*)\\s(?!\\s*=)");
int pos = 0;
while ((pos = re.indexIn(line, pos)) != -1) {
result << re.cap(1);
pos += re.matchedLength();
}
qDebug() << result;
// ("v1=", "v2 = value2", "v3=")
QRegExp re("(\\w+\\s*=\\s*\\w*)\\s(?!\\s*=)");
QStringList result;
int pos = 0;
while ((pos = re.indexIn(line, pos)) != -1) {
result << re.cap(1);
pos += re.matchedLength();
}
qDebug() << result;
// ("v1=", "v2 = value2", "v3=")
To copy to clipboard, switch view to plain text mode
but I can almost guarantee that it will fail for some other input.
Parsing dirty data is not trivial. Often the only way is to pull the text apart and code the rules and exceptions.
Bookmarks