Results 1 to 3 of 3

Thread: Problem regarding regular expression

  1. #1
    Join Date
    Nov 2013
    Location
    Chandigarh, India
    Posts
    62
    Thanks
    8
    Thanked 11 Times in 7 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Problem regarding regular expression

    I am using some regular expressions in my application. I am making changes to the application mentioned below-:


    http://qt-project.org/doc/qt-4.8/ric...ghlighter.html

    Making changes to the highlighter.cpp file of the application, I have done something like this :-

    Qt Code:
    1. QTextCharFormat plus,quotationformat;
    2.  
    3. quotationFormat.setForeground(Qt::blue);
    4. rule.pattern = QRegExp("\".*\"");
    5. rule.format = quotationFormat; // for double quotes
    6. highlightingRules.append(rule);
    7.  
    8.  
    9. plus.setForeground(Qt::red);
    10. rule.pattern = QRegExp("\\+");
    11. rule.format = plus;
    12. highlightingRules.append(rule);
    To copy to clipboard, switch view to plain text mode 


    The problem I am facing is that if '+' is written inside double quotes , the quotation format rules does not apply on it. Something like this-:
    If I write + outside quotes, it is displayed in red like-:

    Adding 1 + 2 makes 3.

    But If I write it inside quotes like-:

    "Adding 1 + 2 makes 3...."
    ..

    I want that + should also appear in blue when i type it inside quotes. Can anyone help me what mistake I am making??

  2. #2
    Join Date
    Oct 2011
    Posts
    27
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem regarding regular expression

    Hi,

    What do you think about adding a precedence tag to the HighlightingRule structure and then use it in void Highlighter::highlightBlock(const QString &text) to determine whether an highlighting rule should be applied or not ?

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem regarding regular expression

    Quote Originally Posted by aaditya190 View Post
    Can anyone help me what mistake I am making??
    The problem has nothing to do with the title of the thread.

    The highlightBlock() function does the heavy lifting by matching all patterns in the order provided and applying the format specified for each match. The way the example is coded it applies the quoted pattern first, then the plus pattern. It doesn't apply just the first pattern matched, or the longest pattern matched, or the "most sensible" pattern matched unless you code it to do so. Open your debugger and single step through that function to see what I mean.

    You might get away with applying the rules in opposite order but I expect you have other rules that cannot be "fixed" in the same way. Implementing more complex rules will require more complex code on your part. Thomas@itest had some suggestions but there is a multitude of ways you might want or need to approach your particular problem. You cannot cut and paste your way to a fully fledged syntax highlighting code editor.
    Last edited by ChrisW67; 12th December 2013 at 22:12.

Similar Threads

  1. Problem with regular expression not working
    By Suppaman in forum Qt Programming
    Replies: 6
    Last Post: 9th April 2013, 22:40
  2. Regular expression
    By aguleo in forum Newbie
    Replies: 0
    Last Post: 25th January 2013, 15:00
  3. Regular expression help!
    By ConkerX in forum Qt Programming
    Replies: 10
    Last Post: 31st August 2011, 15:47
  4. Help with regular expression
    By Gourmet in forum General Programming
    Replies: 19
    Last Post: 11th August 2011, 15:04
  5. Regular Expression Problem
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2009, 09:41

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.