Please correct this regular QRegExp PIDRegExp(attr + "Rev_(.+)[&\\]"); : QRegExp PIDRegExp(attr + "Rev_(.+)[&\\]"); It is necessary that from this: G&Rev_1100\1 this: 1100 instead of the symbol \ may be another symbol & therefore put [&\\]
- oneAnd what is being captured now? - Alex Belyaev
2 answers
In Qt, regular expressions are slightly different from those familiar to us in PHP, C #, Python ...
In order to find a backslash using a regular expression, 4 such signs must be used, since there can be screened sequences in the Qt lines:
QRegExp PIDRegExp(attr + "Rev_(.+)[&\\\\]"); PIDRegExp.setMinimal(true); // Отключаем жадность в Qt To do without "turning off greed," you can use [^&\\\\]+ , which means everything except & and \ :
QRegExp PIDRegExp(attr + "Rev_([^&\\\\]+)"); In my opinion, such an expression is unnecessary, since you need to extract the numbers after Rev_ .
QString str = "G&Rev_1100\\1"; QRegExp rxlen("Rev_(\\d+)"); // Тут обратная косая черта двойная int pos = rxlen.indexIn(str); QStringList list = rxlen.capturedTexts(); // Получаем массив захваченных подстрок QString my_number = list[1]; // А вот и наше число - Emm ... Register-dependent and greed is not the same thing :) Then
QRegExp::setMinimal(bool minimal)( Tynz ) is really a Qt-way. - Alex Belyaev - I apologize, this is not what I pasted: (Corrected. - Wiktor Stribiżew
The question is, on what grounds 1100 is distinguished from the expression, which is generally a group of characters.
If these are always numbers, then the most obvious way would be to explicitly designate this in the regular schedule: G&Rev_(\d+)[&\\]1 (one or more numbers fall into the group). So it will be including more vividly for the person who will then support this code.
If the characters can be any, just to follow the "closing" trait, and the use .* was consciously and inevitably, then such an expression may be due to greed. Try the lazy type option instead .*? .
However, your option should have worked fine: having eagerly captured the group 1100\1 , the regular team understands that the completion of \1 does not work at all, and takes a step back. Thus, reaching version 1100 and following it \1 . It is also not clear why you use * . Maybe a sequence of characters, and maybe immediately G&Rev_\1 ? Think about it yourself :)