{
// regex explaination
//
// /(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)/g
// (?:^|,) Non-capturing group
// 1st Alternative: ^
// ^ assert position at start of the string
// 2nd Alternative: ,
// , matches the character , literally
// 1st Capturing group (\"(?:[^\"]+|\"\")*\"|[^,]*)
// 1st Alternative: \"(?:[^\"]+|\"\")*\"
// \" matches the character " literally
// (?:[^\"]+|\"\")* Non-capturing group
// Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
// 1st Alternative: [^\"]+
// [^\"]+ match a single character not present in the list below
// Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
// \" matches the character " literally
// 2nd Alternative: \"\"
// \" matches the character " literally
// \" matches the character " literally
// \" matches the character " literally
// 2nd Alternative: [^,]*
// [^,]* match a single character not present in the list below
// Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
// , the literal character ,
// g modifier: global. All matches (don't return on first match)
//
QString regex
= "(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)";
regex.replace(",", delimiter);
QRegularExpression re(regex);
if (temp.right(1) == "\n") temp.chop(1);
QRegularExpressionMatchIterator it = re.globalMatch(temp);
while (it.hasNext())
{
QRegularExpressionMatch match = it.next();
if (match.hasMatch())
{
field = match.captured(1);
if (field.left(1) == "\"" && field.right(1) == "\"")
field = field.mid(1, field.length()-2);
field_list.push_back(field);
}
}
return field_list;
}
QStringList TableSync::parseCsvFields(const QString &line, const QChar delimiter)
{
QString temp = line;
QString field;
QStringList field_list;
// regex explaination
//
// /(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)/g
// (?:^|,) Non-capturing group
// 1st Alternative: ^
// ^ assert position at start of the string
// 2nd Alternative: ,
// , matches the character , literally
// 1st Capturing group (\"(?:[^\"]+|\"\")*\"|[^,]*)
// 1st Alternative: \"(?:[^\"]+|\"\")*\"
// \" matches the character " literally
// (?:[^\"]+|\"\")* Non-capturing group
// Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
// 1st Alternative: [^\"]+
// [^\"]+ match a single character not present in the list below
// Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
// \" matches the character " literally
// 2nd Alternative: \"\"
// \" matches the character " literally
// \" matches the character " literally
// \" matches the character " literally
// 2nd Alternative: [^,]*
// [^,]* match a single character not present in the list below
// Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
// , the literal character ,
// g modifier: global. All matches (don't return on first match)
//
QString regex = "(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)";
regex.replace(",", delimiter);
QRegularExpression re(regex);
if (temp.right(1) == "\n") temp.chop(1);
QRegularExpressionMatchIterator it = re.globalMatch(temp);
while (it.hasNext())
{
QRegularExpressionMatch match = it.next();
if (match.hasMatch())
{
field = match.captured(1);
if (field.left(1) == "\"" && field.right(1) == "\"")
field = field.mid(1, field.length()-2);
field_list.push_back(field);
}
}
return field_list;
}
To copy to clipboard, switch view to plain text mode
Hope that helps.
Bookmarks