PDA

View Full Version : Regular Expression to match preprocessor directive statement



parulkalra
25th November 2013, 13:09
What is Regular Expression for matching Preprocessor directive statements like #include <stdio.h>?

stampede
25th November 2013, 13:31
Don't you think one thread about regular expressions is enough ? link to previous thread (http://www.qtcentre.org/threads/57021-Regular-Expression-to-match-Parenthesis-(-)-Brackets-(-()-))
I have a feeling that you want us to do your homework.
Please show us what have you tried so far.

parulkalra
26th November 2013, 06:11
i tried these code but they are not working :

functionFormat1.setFontItalic(false);
functionFormat1.setForeground(Qt::green);
rule.pattern = QRegExp("\\b#([A-Za-z0-9]+ <>)");
rule.format = functionFormat1;
highlightingRules.append(rule);


functionFormat1.setFontItalic(false);
functionFormat1.setForeground(Qt::green);
rule.pattern = QRegExp("\\b[#]([A-Za-z0-9])");
rule.format = functionFormat1;
highlightingRules.append(rule);

ChrisW67
27th November 2013, 23:33
Read the expressions out in plain English (or your fave language) and I think it is obvious why they will not work (for some definition of work):


#include <stdio.h>


The first expression reads:

A word boundary (matched)
Followed by # (matched)
Followed by one or more letters from the set [A-Za-z0-9] (matches "include")
Followed by a single space (matched)
Followed by a less than symbol (matched)
Followed by a greater than symbol (NOT MATCHED)


The second expression reads:

A word boundary (matched)
Followed by a single character from the set [#] (matched inefficiently)
Followed by a single character from the the set [A-Za-z0-9] (matches "i")

This matches the input but not the whole input.