
Originally Posted by
bnosam
Is there an easier way to do this, because potentially there could be many strings in it that I don't want the spaces removed from. I'm not even sure how to approach this.
Like:
@BEGIN:4; 17, 1;1, "This is some text right here."; 3; 18;1, "This is more text."; 3; 18;1, "Another set of text right here."; 3; 2; 5;11, LABEL;end;@LABEL:7, 1;8;end;@END
@BEGIN:4; 17, 1;1, "This is some text right here."; 3; 18;1, "This is more text."; 3; 18;1, "Another set of text right here."; 3; 2; 5;11, LABEL;end;@LABEL:7, 1;8;end;@END
To copy to clipboard, switch view to plain text mode
Here is one more version that uses regular expressions and removes the kludge used to skip over escaped quotes (the regex doesn't match on escaped quotes):
// remove spaces in string outside of quotes
QString str
= "@BEGIN:4; 17, 1;1, \"This is some text right here.\"; 3; 18;1, \"This is more text.\"; 3; 18;1, \"Another set of text right here.\"; 3; 2; 5;11, LABEL;end;@LABEL:7, 1;8;end;@END";
qDebug("....................................................................................................1.........1.........1.........1.........1.........1.........1.........1.........");
qDebug("..........1.........2.........3.........4.........5.........6.........7.........8.........9.........0.........1.........2.........3.........4.........5.........6.........7.........");
qDebug("012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789");
qDebug("%s", qPrintable(str));
int pos, q1, q2 = 0;
QRegularExpression re("(?<![\\\\])\"");
pos = 0;
q1 = str.indexOf(re);
if (q1 == -1)
{
// no quotes in string, so just remove all spaces
outstr += str.mid(0).remove(" ");
}
else
{
while (q1 != -1 && q2 != -1)
{
q2 = str.indexOf(re, q1+1);
if (q2 == -1)
{
// unbalanced quotes, so strip all spaces from current pos to end of string (or return an error, etc)
outstr += str.mid(pos).remove(" ");
}
else
{
// found balanced quote, so strip spaces before quote and append
// quoted portion, then look for next quoted section and continue looping
outstr += str.mid(pos, q1-pos).remove(" ");
outstr += str.mid(q1,q2-q1+1);
pos = q2 + 1;
q1 = str.indexOf(re, pos);
if (q1 == -1)
{
// no more quoted text, remove spaces from rest of string
outstr += str.mid(pos).remove(" ");
}
}
}
}
qDebug("%s", qPrintable(outstr));
// remove spaces in string outside of quotes
QString str = "@BEGIN:4; 17, 1;1, \"This is some text right here.\"; 3; 18;1, \"This is more text.\"; 3; 18;1, \"Another set of text right here.\"; 3; 2; 5;11, LABEL;end;@LABEL:7, 1;8;end;@END";
QString outstr;
qDebug("....................................................................................................1.........1.........1.........1.........1.........1.........1.........1.........");
qDebug("..........1.........2.........3.........4.........5.........6.........7.........8.........9.........0.........1.........2.........3.........4.........5.........6.........7.........");
qDebug("012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789");
qDebug("%s", qPrintable(str));
int pos, q1, q2 = 0;
QRegularExpression re("(?<![\\\\])\"");
pos = 0;
q1 = str.indexOf(re);
if (q1 == -1)
{
// no quotes in string, so just remove all spaces
outstr += str.mid(0).remove(" ");
}
else
{
while (q1 != -1 && q2 != -1)
{
q2 = str.indexOf(re, q1+1);
if (q2 == -1)
{
// unbalanced quotes, so strip all spaces from current pos to end of string (or return an error, etc)
outstr += str.mid(pos).remove(" ");
}
else
{
// found balanced quote, so strip spaces before quote and append
// quoted portion, then look for next quoted section and continue looping
outstr += str.mid(pos, q1-pos).remove(" ");
outstr += str.mid(q1,q2-q1+1);
pos = q2 + 1;
q1 = str.indexOf(re, pos);
if (q1 == -1)
{
// no more quoted text, remove spaces from rest of string
outstr += str.mid(pos).remove(" ");
}
}
}
}
qDebug("%s", qPrintable(outstr));
To copy to clipboard, switch view to plain text mode
And the output that shows the original string and the output string after removing spaces (with a header to show column offset):
....................................................................................................1.........1.........1.........1.........1.........1.........1.........1.........
..........1.........2.........3.........4.........5.........6.........7.........8.........9.........0.........1.........2.........3.........4.........5.........6.........7.........
012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@BEGIN:4; 17, 1;1, "This is some text right here."; 3; 18;1, "This is more text."; 3; 18;1, "Another set of text right here."; 3; 2; 5;11, LABEL;end;@LABEL:7, 1;8;end;@END
@BEGIN:4;17,1;1,"This is some text right here.";3;18;1,"This is more text.";3;18;1,"Another set of text right here.";3;2;5;11,LABEL;end;@LABEL:7,1;8;end;@END
....................................................................................................1.........1.........1.........1.........1.........1.........1.........1.........
..........1.........2.........3.........4.........5.........6.........7.........8.........9.........0.........1.........2.........3.........4.........5.........6.........7.........
012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@BEGIN:4; 17, 1;1, "This is some text right here."; 3; 18;1, "This is more text."; 3; 18;1, "Another set of text right here."; 3; 2; 5;11, LABEL;end;@LABEL:7, 1;8;end;@END
@BEGIN:4;17,1;1,"This is some text right here.";3;18;1,"This is more text.";3;18;1,"Another set of text right here.";3;2;5;11,LABEL;end;@LABEL:7,1;8;end;@END
To copy to clipboard, switch view to plain text mode
Bookmarks