Results 1 to 20 of 20

Thread: Help with regular expression

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Help with regular expression

    I don't have time to look this up, so you'll want to check syntax, but something like

    ^[" ' ; . , : ( ) { }\ [\ ]](if|==|!=|=|+|-|hello_world|<whatever else>)^[" ' ; . , : ( ) { }\ [\ ]]

    should get you started. Note that the order of the items in the or'd middle clause is important; you generally want the more complex expressions which contain following simpler expressions to come first so they get matched before their simpler counterpart. The enclosing parentheses will let you extract the bit that matched. Note that members of the OR clause can themselves be REs; you need to pay close attention to the matching rules in such cases, though.

    It would probably be simpler to tokenize on the (rather huge) list of delimiters, then simply check string equality against your list of keywords rather than using REs in this case.

  2. #2
    Join Date
    Apr 2011
    Posts
    31
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Help with regular expression

    The sequence of patterns cannot be used. It must work in the cycle on patterns list like this:

    Qt Code:
    1. foreach( QString pattern, wordsQStringList )
    2. {
    3. rule.pattern = QRegExp( "\\b(" + pattern + ")\\b" );
    4. rule.format = format;
    5. highlightRules.append( rule );
    6. }
    To copy to clipboard, switch view to plain text mode 

    This is mandatory because of other highlighting algorithm parts. The very complex rule will slow down redrawing of large texts, up to 5000-6000 lines.

    That is why I'm still confused
    Last edited by Gourmet; 9th August 2011 at 18:35.

  3. #3
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Help with regular expression

    Then do the same thing suggested above, but in a loop.

    I find it very hard to believe, though, that RE evaluation will be slower than a loop checking against all possibilities, even if the loop short-circuits. Especially the way you've formulated it here; you'll wind up re-compiling the RE on each iteration.

  4. #4
    Join Date
    Apr 2011
    Posts
    31
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Help with regular expression

    Then do the same thing suggested above, but in a loop.
    But HOW? This

    Qt Code:
    1. foreach( QString pattern, wordsQStringList )
    2. {
    3. rule.pattern = QRegExp( "^[\"';.,:(){}\[\]](" + pattern + ")^[\"';.,:(){}\[\]]" );
    4. rule.format = format;
    5. highlightRules.append( rule );
    6. }
    To copy to clipboard, switch view to plain text mode 

    doesn't work.

    About the speed - it would be better of course test which would be faster. This greatly depends from internal indexIn() implementation. But this idea use it cyclically taken from native Qt examples.

    Number of words could be logically unlimited (now there about 200).
    Last edited by Gourmet; 9th August 2011 at 20:47.

  5. #5
    Join Date
    Jul 2011
    Posts
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with regular expression

    1. speed is O(n) you shouldnt be complaining.
    2. each punctuational delimiter should be pretended with a '\'
    3.have you even given thought about my implementation?

    from what i gather you a looking for a "word" surrounded by some sort of delimiters?
    and only capture Exact Matches?
    word = sub, thus capture sub but not submarine?

    Qt Code:
    1. QEegExp exp = QString( " exp " );
    2. foreach( word, wordlist)
    3. {
    4. if( exp.exactMatch(word) )
    5. {
    6. do something with the matched "word"
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Read up on QRegExp, reserved characters ect:
    http://doc.qt.nokia.com/latest/qregexp.html

    What is expected input just a giant string of characters that you need to split up?
    what is expected output?

  6. #6
    Join Date
    Apr 2011
    Posts
    31
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Help with regular expression

    What is expected input just a giant string of characters that you need to split up?
    a program code on specific (not widely known) programming language placed inside QPlainTextEdit

    what is expected output?
    highlighted keywords of this language, they are placed inside wordsQStringList - this is mandatory and cannot be changed (the most important reason is - number of keywords can be different and they can be written on different human languages, not only by Latin characters and can include not only letters or digits, it is imported from outside highlighter and must be used inside highlighter)

    highlighting is implemented as

    Qt Code:
    1. void Syntaxer::highlightBlock(const QString &text)
    2. {
    3. foreach( HighRule rule, highlightRules )
    4. {
    5. QRegExp expression( rule.pattern );
    6. int index = expression.indexIn( text );
    7. while( index >= 0 )
    8. { // probably there is bug in indexIn, it returns zero even if text not found
    9. int length = expression.matchedLength();
    10. if( !length )
    11. break;
    12. setFormat( index, length, rule.format );
    13. index = expression.indexIn( text, index + length );
    14. }
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    as recommended in Qt examples. The exactMatch cannot be used cause text can contain different number of keywords.

    all other things work fine, they are debugged and cannot be changed - I only need create a rule to highlight those keywords, but I'm not a keen in Regular Expressions
    Last edited by Gourmet; 9th August 2011 at 21:55.

  7. #7
    Join Date
    Jul 2011
    Posts
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with regular expression

    I would just recommend sitting down and reading the QT entry on QRegExp

    And Play around with some on this simple tester: http://myregextester.com/index.php

  8. #8
    Join Date
    Apr 2011
    Posts
    31
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Help with regular expression

    I read this all carefully. If I could make expression by myself I would not ask for advice. I'm already tired after hours of attempts. I have to LOTS of other things but I stalled on this.
    Last edited by Gourmet; 9th August 2011 at 22:27.

  9. #9
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Help with regular expression

    If you're trying to parse a language, this is probably the worst way imaginable to do it. Go read about lex and yacc if you want to do this properly.

    The exactMatch cannot be used cause text can contain different number of keywords.
    So what? Every keyword is separated by delimiters and is considered alone. ExactMatch is EXACTLY what you want to use. Any of the solutions already discussed above will work just fine, although the resulting code will be a maintenance nightmare. To reiterate:

    1) Split your string into individual keywords.

    2) Match each keyword against the list of target keywords.

    Show your code for step one. This should be a one-liner in Qt, or a very small loop in C/C++. Provide an actual sample of text and target list; they way these keep changing in your descriptions makes me almost certain there's more to know here.

  10. #10
    Join Date
    Apr 2011
    Posts
    31
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Help with regular expression

    1) Split your string into individual keywords.

    2) Match each keyword against the list of target keywords.
    Not applicable, this will too slow. The solution must dance not from keywords but from delimiters. They are known and have very limited number. All other character sequences are keywords.

    But exactly the code to find delimiters can be almost the same as for step 1). And it can be based on indexIn() call.
    Last edited by Gourmet; 10th August 2011 at 09:22.

  11. #11
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Help with regular expression

    You have no idea at all how "slow" this approach will be, because right now the performance of YOUR algorithm is infinitely bad - it produces absolutely no results no matter how long you wait.

    How about actually IMPLEMENTING something instead of whining about speed you can't even properly measure because you have nothing to compare it to? Only then will you be able to tell if performance is acceptable or not, and only then will you be able to compare performance of one approach over another.

    Hint: you are making this simple problem much, much, much too hard. Stop generating excuses and generate some code, instead.

    I'm not seeing any actual code, text or keyword examples. Obviously, the code doesn't exist, but the text and keyword examples are critical to understanding what your problem actually is, given that your descriptions are vague and continue to change.
    Last edited by SixDegrees; 10th August 2011 at 09:58.

  12. #12
    Join Date
    Apr 2011
    Posts
    31
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Help with regular expression

    I described problem and described a possible solution which I need. If you cannot help - then just do not troll me... Ignoring.

  13. #13
    Join Date
    Jul 2011
    Posts
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with regular expression

    Quote Originally Posted by Gourmet View Post
    Not applicable, this will too slow. The solution must dance not from keywords but from delimiters. They are known and have very limited number. All other character sequences are keywords.
    QStringList keywords = inputTextLine.split( QRegExp( all delimiters or'd) )

    Ta freaking Da.
    No one is trolling you here, Six is correct you keep complaining, and making excuses. If you are not willing to learn via trial and error you will not learn at all. thus making me wonder why you are taking a programming class

  14. #14
    Join Date
    Apr 2011
    Posts
    31
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Help with regular expression

    I'm not taking programming class. I'm experienced enough in C/C++ and OOP after hundreeds of thousands working lines since 1989. Currently I debug up to 400-500 lines of C++ code per day using wide set of Qt classes. But I never used Perl-like regular expressions. That is why I asked for help but not to try "teach" me something... If I told - I need this kind of solution - that means only: I need this kind of solution. Not other kind but THIS KIND. A pattern to find keyword in string with known delimiters. I did not ask for help in coding. All other suggestions I could engineer by myself and not ask for help. If the solution I asked for is impossible - just tell: "it is impossible". Then I'll find other solution by myself without help. Or tell: "Sorry I cant' help you".

    Do not drop a wood bar if you have lifebuoy - drop bar only if you don't have lifebuoy. And notify about it mandatory. Otherwise drop lifebuoy but not bar. And never shout: "Teach swim yourself".
    Last edited by Gourmet; 10th August 2011 at 16:19.

Similar Threads

  1. Regular Expression Problem
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2009, 09:41
  2. set a regular expression on QTextEdit
    By mattia in forum Newbie
    Replies: 3
    Last Post: 27th March 2008, 10:16
  3. Regular expression in QLineEdit?
    By vishal.chauhan in forum Qt Programming
    Replies: 3
    Last Post: 1st October 2007, 10:58
  4. Find with Regular Expression?
    By vishal.chauhan in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2007, 14:44
  5. How to get a QString from a file (use regular expression)
    By fengtian.we in forum Qt Programming
    Replies: 16
    Last Post: 31st May 2007, 11:06

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
  •  
Qt is a trademark of The Qt Company.