Regular expression for highlighting words in quotes int qt5 - regex

I use QHighlighter class, and used regExp to highlight words in quotes:
void Highlighter::highlightBlock(const QString &text)
{
QRegExp expr("\"(.*?)\"");
int index = expr.indexIn(text);
while(index >=0)
{
int length = expr.matchedLength();
setFormat(index, length, Qt::red);
index = expr.indexIn(text, index+length);
}
}
It doesn't work. Work this:
"\".*\""
But it highlights unnecessary. What regular expression is correct?

Just higlight everything between quotes
QRegExp("\"([^\"]*)\"");
highlight single words (run in loop with offset to match words)
QRegExp("\"(\\w)*\"");

How to match words in quotes:
('|")[^\1]*?\1
Example:
http://regex101.com/r/iF5aA1

Related

Extract the string between two words using RegEx in QT [duplicate]

can anybody help me with this?
I have a string which contains N substrings, delimited by tags and I have to get ALL of the substrings. The string is like
STARTfoo barENDSTARThi there!ENDSTARTstackoverflowrulezEND
I would like to get all the strings between START/END tags, I tried with a couple of regular expressions with no luck:
(START)(.*)(END) gives me ALL the contend between the first and last tag
(START)(\w+)(END) gives me no result
The code is much simple:
QString l_str "STARTfoo barENDSTARThi there!ENDSTARTstackoverflowrulezEND";
QRegExp rx("(START)(\w+)(END)");
QStringList list;
int pos = 0;
while ((pos = rx.indexIn(l_str, pos)) != -1)
{
list << rx.cap(1);
pos += rx.matchedLength();
}
qWarning() << list;
I'd like a resulting list like:
STARTfoo barEND
STARThi there!END
STARTstackoverflowrulezEND
Any help?
Thanks!
Use rx.setMinimal(true) with .* to make it lazy:
QRegExp rx("START.*END");
rx.setMinimal(true);
See the QRegExp::setMinimal docs:
Enables or disables minimal matching. If minimal is false, matching is greedy (maximal) which is the default.

Find strings between two tags with regex in Qt

can anybody help me with this?
I have a string which contains N substrings, delimited by tags and I have to get ALL of the substrings. The string is like
STARTfoo barENDSTARThi there!ENDSTARTstackoverflowrulezEND
I would like to get all the strings between START/END tags, I tried with a couple of regular expressions with no luck:
(START)(.*)(END) gives me ALL the contend between the first and last tag
(START)(\w+)(END) gives me no result
The code is much simple:
QString l_str "STARTfoo barENDSTARThi there!ENDSTARTstackoverflowrulezEND";
QRegExp rx("(START)(\w+)(END)");
QStringList list;
int pos = 0;
while ((pos = rx.indexIn(l_str, pos)) != -1)
{
list << rx.cap(1);
pos += rx.matchedLength();
}
qWarning() << list;
I'd like a resulting list like:
STARTfoo barEND
STARThi there!END
STARTstackoverflowrulezEND
Any help?
Thanks!
Use rx.setMinimal(true) with .* to make it lazy:
QRegExp rx("START.*END");
rx.setMinimal(true);
See the QRegExp::setMinimal docs:
Enables or disables minimal matching. If minimal is false, matching is greedy (maximal) which is the default.

Qt- checking regular expression width

I'm trying to replace regular expression with different expression which depend on width of line and width of box.
Here is my code:
//mangledText is my text that I've searching on it
//rx is regular expression
QRegExp rx("<lms([^<]*)/>");
while ((pos = rx.indexIn(mangledText)) != -1){
for (int j = 0; j < tempLayout->lineCount(); j++){
QTextLine tl = tempLayout->lineAt(j);
//here is width of each line
int naturalTextWidth = tl.naturalTextWidth();
//rect width is maximum width of box
if (naturalTextWidth < rectWidth)
mangledText.replace(pos, rx.matchedLength(), "replace Text");
else
mangledText.replace(pos, rx.matchedLength(), "\n replace Text");
}
}
mangledText.replace('\n', QChar::LineSeparator);
I want to replace regular expression with "\n replace Text" if text on that line is out of box. otherwise I replace it with "replace Text" . Problem is it will always shift it to next line. because rectWidth is smaller that naturalTextWidth. but I want to check an each regular expression to replace.
UPDATED:
For example :
111111111111111111111111111<lms8><lms3><lms2>
is showing :
111111111111111111111111111
<lms8>
<lms3>
<lms2>
and I want this:
111111111111111111111111111
<lms8><lms3><lms2>
Any suggestion?
Try this.
(.[^<]*)(<lms.*>)
Replace with following syntax.
$1\n$2
The result would be
111111111111111111111111111
<lms8><lms3><lms2>

How to QRegExp "[propertyID="anything"] "?

I am parsing a file which contains following packets:
[propertyID="123000"] {
fillColor : #f3f1ed;
minSize : 5;
lineWidth : 3;
}
To scan just this [propertyID="123000"] fragment I havre this QRegExp
QRegExp("^\b\[propertyID=\"c+\"\]\b");
but that does not work? Here I have example code to parse that file above:
QRegExp propertyIDExp= QRegExp("\\[propertyID=\".*\"]");
propertyIDExp.setMinimal(true);
QFile inputFile(fileName);
if (inputFile.open(QIODevice::ReadOnly))
{
QTextStream in(&inputFile);
while (!in.atEnd())
{
QString line = in.readLine();
// if does not catch if line is for instance
// [propertyID="123000"] {
if( line.contains(propertyIDExp) )
{
//.. further processing
}
}
inputFile.close();
}
QRegExp("\\[propertyID=\".+?\"\\]")
You can use ..It will match any character except newline.Also use +? to make it non greedy or it will stop at the last instance of " in the same line
Use the following expression:
QRegExp("\\[propertyID=\"\\d+\"]");
See regex demo
In Qt regex, you need to escape regex special characters with double backslashes, and to match digits, you can use the shorthand class \d. Also, \b word boundary prevented your regex from matching since it cannot match between the string start and [ and between ] and a space (or use \B instead).
To match anything in between quotes, use a negated character class:
QRegExp("\\[propertyID=\"[^\"]*\"]");
See another demo
As an alternative, you can use lazy dot matching with the help of .* and QRegExp::setMinimal():
QRegExp rx("\\[propertyID=\".*\"]");
rx.setMinimal(true);
In Qt, . matches any character including a newline, so please be careful with this option.

Qt5 Qregexp : why my pattern can't work?

I get this problem When I open a text file, I can't get any matched string. Then I test this pattern: .* but I can either get nothing. I'm sure the text file can be read, and the pattern can be accepted in grep. Thank you.
QList<Nmap_result> ans;
QFile file(path);
if(!file.open(QFile::ReadOnly|QFile::Text))
{
exit(1);
}
QString text = file.readAll();
QRegExp reg(QRegExp::escape(".*"));
reg.indexIn(text);
qDebug()<<reg.capturedTexts().join("|")<<endl<<reg.captureCount()<<endl;
Sorry, I should not use escape. But when I change it like this:
QString text = file.readAll();
qDebug()<<text<<endl;
QRegExp reg("[0-9]");
//reg.indexIn(text); //first bind expr test
reg.exactMatch(text); //second bind expr test
qDebug()<<reg.capturedTexts().join("|!!!!!|")<<endl<<reg.captureCount()<<endl;
I use
reg.indexIn(text);
to bind this string to regexp, it return a number,but when I use the next expr
reg.exeacMatch(text);
I get nothing.
Why do you call QRegExp::escape method ?
Try this instead:
QRegExp reg(".*");
Calling QRegExp::escape, your regular expression becomes similar to this string: "\\.\\*". This string indicates that you want to match a dot immediatly followed by a star. This is not the intented use here: match zero or more characters (.*).