我正在使用QRegExp解析电子邮件标题我的问题是如果标题标记是多行的,那么我的正则表达式将不起作用。
这是我的正则表达式:(我现在有\ r \ n有占位符),
QRegExp regex("([\\w-]+): (.+)\\r\\n(?:([^:]+)\\r\\n)?")
regex.setMinimal(true)
// PCRE: ([\w-]+): (.+?)\\r\\n(?:([^:]+?)\\r\\n)?
我正在尝试解析的内容:
MIME-Version: 1.0\r\n
x-no-auto-attachment: 1\r\n
Received: by 10.200.36.132; Sun, 5 Feb 2017 01:21:33 -0800 (PST)\r\n
Date: Sun, 5 Feb 2017 01:21:33 -0800\r\n
Message-ID: <IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII@mail.gmail.com>\r\n
Subject: =?UTF-8?Q?MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM?=\r\n
=?UTF-8?Q?ail?=\r\n
From: =?UTF-8?B?VGhlIGZ1Y2sgYXJlIHUgbG9va2luZyBmb3I/?= <noreply@mail.com>\r\n
To: mail mail <mail@mail.com>\r\n
Content-Type: multipart/alternative; boundary=1a3xca651sv561fd321c5xv61sd12\r\n
它的工作方式与php,js相同......但不适用于QRegExp https://regex101.com/r/0J2jXT/2。我无法获得标签Subject的第二行。
编辑:奇怪的是,如果我使用来自c ++ 11的std :: regex,我会得到正确的结果! http://coliru.stacked-crooked.com/a/93494669f24422e1
答案 0 :(得分:1)
QRegExp
是一个旧类,不应该再使用了(除了你被迫使用Qt4 ......)。如果您可以使用Qt 5并希望使用QRegularExpression
更好的性能。有了它,你的代码就可以了:
QString data = "Message-ID: <IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII@mail.gmail.com>\r\n"
"Subject: =?UTF-8?Q?MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM?=\r\n"
"=?UTF-8?Q?ail?=\r\n"
"From: =?UTF-8?B?VGhlIGZ1Y2sgYXJlIHUgbG9va2luZyBmb3I/?= <noreply@mail.com>\r\n";
QRegularExpression rx("([\\w-]+): (.+)\\r\\n(?:([^:]+)\\r\\n)?");
QRegularExpressionMatchIterator it = rx.globalMatch(data);
while(it.hasNext()) {
QRegularExpressionMatch match = it.next();
qDebug() << match.capturedTexts();
}
输出:
("Message-ID: <IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII@mail.gmail.com>\r\n", "Message-ID", "<IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII@mail.gmail.com>")
("Subject: =?UTF-8?Q?MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM?=\r\n=?UTF-8?Q?ail?=\r\n", "Subject", "=?UTF-8?Q?MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM?=", "=?UTF-8?Q?ail?=")
("From: =?UTF-8?B?VGhlIGZ1Y2sgYXJlIHUgbG9va2luZyBmb3I/?= <noreply@mail.com>\r\n", "From", "=?UTF-8?B?VGhlIGZ1Y2sgYXJlIHUgbG9va2luZyBmb3I/?= <noreply@mail.com>")