与Perl兼容的正则表达式匹配大写“,”,空格和“(”“)”

时间:2016-04-28 11:28:31

标签: regex qt5 pcre

我正在尝试从SQL中删除QString语句。所有SQL指令均为大写。我希望只有列名全部小写,下划线作为名称中的空格。作为示例,QString可能包含:

CREATE TABLE IF NOT EXISTS activated_alerts(id INTEGER PRIMARY KEY AUTOINCREMENT, date_and_time TEXT, system_state TEXT, alarm_no_185 BOOL NOT NULL, alarm_no_4 BOOL NOT NULL, alarm_no_5 BOOL NOT NULL, alarm_no_2 BOOL NOT NULL, alarm_no_102 BOOL NOT NULL, alarm_no_7 BOOL NOT NULL, alarm_no_27 BOOL NOT NULL, alarm_no_120 BOOL NOT NULL, alarm_no_103 BOOL NOT NULL, alarm_no_190 BOOL NOT NULL, alarm_no_30 BOOL NOT NULL, alarm_no_31 BOOL NOT NULL, alarm_no_32 BOOL NOT NULL, alarm_no_33 BOOL NOT NULL, alarm_no_34 BOOL NOT NULL, alarm_no_35 BOOL NOT NULL)

我尝试了^[A-Z]+$,但我得到了零匹配。

QRegularExpression re("^[A-Z]+$");
QRegularExpressionMatch match = re.match(m_activatedAlertsSQLStatement);
if(match.hasMatch()) 
{
    qDebug() <<  "CAPTURED: " << match.capturedTexts();
}

1 个答案:

答案 0 :(得分:0)

您可以使用

[^\WA-Z]+

请参阅regex demo。该模式只匹配一个或多个不是大写ASCII字母的单词字符。

Qt示例演示:

QRegularExpression re("[^\\WA-Z]+");
QString subject("YOUR_STRING");
QRegularExpressionMatchIterator i = re.globalMatch(subject);
while (i.hasNext()) {
    QRegularExpressionMatch match = i.next();
    qDebug() <<  "CAPTURED: " << match.captured(0); // Print match value
    //...
}