PDA

View Full Version : Regular Expression for Operators



parulkalra14
6th January 2014, 08:13
Can we write a single code for all the operators instead of writing code for each and every operator?
This code is working but i want single regular Expression for all operators
plus.setForeground(Qt::red);
rule.pattern = QRegExp("\\+"); // for plus
rule.format = plus;
highlightingRules.append(rule);

minus.setForeground(Qt::red);
rule.pattern = QRegExp("\\-");
rule.format = minus; // for minus
highlightingRules.append(rule);


mul.setForeground(Qt::red);
rule.pattern = QRegExp("\\*");
rule.format = mul; // for multiply
highlightingRules.append(rule);


div.setForeground(Qt::red);
rule.pattern = QRegExp("/");
rule.format = div; // for division
highlightingRules.append(rule);

anda_skoa
6th January 2014, 10:46
Not sure what you mean, but what about using "OR" clauses, i.e. using | to separate sub patterns?

Cheers,
_

parulkalra14
6th January 2014, 11:37
@anda_skoa Sir : This code is working thanku so much.but actually, I want whenever these operators appears in between double quotes like: " + ", + operator should be in blue color and whenever these operators appears in an expression i.e a=b+c, then + should be red color. But problem comes whenever i write both these codes together in the same program then operators in between double quotes appears in red instead of blue.Can you provide me some code related to this problem?
quotationFormat.setForeground(Qt::blue);
rule.pattern = QRegExp("\".*\"");
rule.format = quotationFormat; // for double quotes
highlightingRules.append(rule);

operators.setForeground(Qt::red);
rule.pattern = QRegExp("\\+|\\-|\\/|\\*");
rule.format = operators;
highlightingRules.append(rule);

anda_skoa
6th January 2014, 14:02
Well, if you have one regex matching an expression that is part of a larger expression matched by a different regex, than you need to care about the order in which you apply your rules.

Cheers,
_

P.S.: if you are doing syntax highlighting make sure you had a look at Kate's syntax highlighting files. Those are also used by Qt creator for example.
http://kate-editor.org/2005/03/24/writing-a-syntax-highlighting-file/

parulkalra14
7th January 2014, 06:46
@anda_soka: Its working now...Thanks alot....:)

Added after 47 minutes:

I have one more query, only few colors are supported by Qt For eg: if I would like to use orange color then what will be the code for that ?

ChrisW67
7th January 2014, 08:30
This thread bears a striking resemblance to this thread, the end of this thread and some others by aaditya190.

Does parulkalra14 (http://www.qtcentre.org/members/53033-parulkalra14) == aaditya190 (http://www.qtcentre.org/members/52660-aaditya190)? Do we have alter egos, two students working the same assignment or (diety forbid) commercial project?


Added after 4 minutes:


I have one more query, only few colors are supported by Qt For eg: if I would like to use orange color then what will be the code for that ?

QTextCharFormat, QTextFormat, QBrush and QColor

Qt Assistant is your friend

wysota
7th January 2014, 10:36
Does parulkalra14 (http://www.qtcentre.org/members/53033-parulkalra14) == aaditya190 (http://www.qtcentre.org/members/52660-aaditya190)? Do we have alter egos, two students working the same assignment or (diety forbid) commercial project?


Same IP :)