请求适配Qt库的正则表达式:提取指定位置的目标数字
Qt-Compatible Regex for Parsing Instructor & Course Schedule Data
Got it, let's tackle this parsing problem with a Qt-friendly regular expression tailored exactly to your requirements. I'll break down the pattern step by step and show you how to implement it in Qt, since you mentioned compatibility with the Qt library is a priority.
First, let's recap the key requirements we need to cover:
- Every line starts with
Instructor :, followed by a numeric ID, an optional name, then one of the specified academic titles (Associate Professor, Assistant Professor, Lecturer, Teacher, Teaching Assistant) - After the keywords
lecture,exercise, orpractical, there are six numeric values—we need to capture the rightmost one - The regex must work seamlessly with Qt's
QRegularExpression(which uses a PCRE-like syntax with minor Qt-specific nuances)
The Regular Expression
Here's the regex pattern we'll use:
^Instructor : (\d+)(?:\s+([\w\s]+?))?\s+(Associate Professor|Assistant Professor|Lecturer|Teacher|Teaching Assistant).*(lecture|exercise|practical)\s+(?:\S+\s+){5}(\S+)
Breakdown of the Pattern
Let's walk through each segment to understand what it does and why it's structured this way:
^Instructor : (\d+)^anchors the match to the start of the line (critical to ensure we only process lines that actually start with the required prefix)(\d+)captures the instructor's numeric ID into Capture Group 1
(?:\s+([\w\s]+?))?(?:...)is a non-capturing group (we don't need to capture the leading whitespace itself)([\w\s]+?)is an optional capture for the instructor's name—+?makes it non-greedy so it stops as soon as it hits the academic title (prevents it from gobbling up the title by mistake). The trailing?makes this entire group optional to handle cases where the name is missing. Captured into Capture Group 2
\s+(Associate Professor|Assistant Professor|Lecturer|Teacher|Teaching Assistant)- Matches one or more whitespace characters, then exactly one of your specified academic titles. Captures the title into Capture Group 3
.*- Matches any characters (non-greedily, by default in Qt) to skip over intermediate content like course names, until we hit the activity keyword (
lectureetc.)
- Matches any characters (non-greedily, by default in Qt) to skip over intermediate content like course names, until we hit the activity keyword (
(lecture|exercise|practical)\s+(?:\S+\s+){5}(\S+)- First, captures the activity type into Capture Group 4 (optional, but useful if you need to track whether it's a lecture/exercise/practical)
(?:\S+\s+){5}matches exactly five whitespace-separated numeric values (non-capturing, since we don't need these intermediate numbers)(\S+)captures the sixth (rightmost) numeric value into Capture Group 5—this is the value you need to extract
Qt Implementation Example
Here's how to integrate this regex into Qt code using QRegularExpression:
#include <QRegularExpression> #include <QString> #include <QDebug> void parseInstructorLine(const QString& inputLine) { // Use Qt's raw string literal to avoid escaping backslashes QRegularExpression regex(R"(^Instructor : (\d+)(?:\s+([\w\s]+?))?\s+(Associate Professor|Assistant Professor|Lecturer|Teacher|Teaching Assistant).*(lecture|exercise|practical)\s+(?:\S+\s+){5}(\S+))"); QRegularExpressionMatch matchResult = regex.match(inputLine); if (matchResult.hasMatch()) { QString instructorId = matchResult.captured(1); QString instructorName = matchResult.captured(2); // Empty if name is missing QString instructorTitle = matchResult.captured(3); QString activityType = matchResult.captured(4); QString targetNumber = matchResult.captured(5); qDebug() << "Instructor ID:" << instructorId; if (!instructorName.isEmpty()) { qDebug() << "Instructor Name:" << instructorName.trimmed(); } qDebug() << "Academic Title:" << instructorTitle; qDebug() << "Activity Type:" << activityType; qDebug() << "Target Numeric Value:" << targetNumber; } else { qDebug() << "Line failed to match the pattern:" << inputLine; } } // Example usage int main() { QString sampleLine = "Instructor : 95371 XXX XXX XXX Associate Professor Course Name EE 311 Microprocessors lecture 834 1 32 3 3 1 08:00 AM - 08:50 AM 1 09:00 AM - 09:50 AM 3 10:00 AM - 10:50 AM 21 Total : 3 Section Position : Serial Campus Hrs Weekly Activity Semester: Time Schedule Type : 411 Reg. Regular First Semester 41/42 Rank : Course"; parseInstructorLine(sampleLine); return 0; }
Quick Adjustment Tips
- If the six numeric values are separated by commas (or a mix of commas and spaces), replace
\s+with[\s,]+in the activity section of the regex - If instructor names include special characters (like hyphens, apostrophes, or periods), update the name capture group from
[\w\s]+?to[\w\s'-]+?to include those characters - Qt's regex engine supports case-insensitive matching if needed—just add
QRegularExpression::CaseInsensitiveOptionto the regex options
内容的提问来源于stack exchange,提问作者CroCo
相关产品推荐
相关产品推荐

