Results 1 to 10 of 10

Thread: Qt regular expressions!!

  1. #1
    Join Date
    Mar 2007
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Qt regular expressions!!

    I was wondering if there is are ways to acess the sub-matches apart from the complete match when we do regular-expression matching. for instance,

    "^[a-z](\d+)" when mached with "abcd1243abc" should not only return the final match i.e "d1234" but also give me access to sub-match "1234" which corresponds to (\d+) which can be done in perl and other scripting languages which support regular expressions using variables like $1, $2 .. etc.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt regular expressions!!


  3. #3
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt regular expressions!!

    QRegExp::capturedTexts() is another solution. The choice of either of these depends on what you want to do... the aforementioned returns a single item at a given index whereas this one returns a list of all captured texts...
    Current Qt projects : QCodeEdit, RotiDeCode

  4. #4
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt regular expressions!!

    Quote Originally Posted by notsonerdysunny View Post
    I was wondering if there is are ways to acess the sub-matches apart from the complete match when we do regular-expression matching. for etc.
    Wath is sub-matches ... on italian , german is this like find each find image on tag .. src="image.png" ?? on text....?

    like this smal piece to replace qt uncool text style css....

    Qt Code:
    1. /* find style,lang,class on a html file && append o a list && and end remove....*/
    2. QString TidyExternalHtml( QString body )
    3. {
    4. QString prehtml = TidyCleanhtml(body); /* base clean to stay on minimal standard xhtml and capable to find .... */
    5. QStringList notneed;
    6. notneed.clear();
    7. ///////////////width="456" lang class
    8. QRegExp expression( "width=[\"\'](.*)[\"\']", Qt::CaseInsensitive ); /* table td tr width image amen */
    9. QRegExp expression2( "style=[\"\'](.*)[\"\']", Qt::CaseInsensitive );
    10. QRegExp expression3( "lang=[\"\'](.*)[\"\']", Qt::CaseInsensitive );
    11. QRegExp expression4( "class=[\"\'](.*)[\"\']", Qt::CaseInsensitive );
    12.  
    13.  
    14. expression.setMinimal(true);
    15. expression2.setMinimal(true);
    16. expression3.setMinimal(true);
    17. expression4.setMinimal(true);
    18.  
    19. int iPosition = 0;
    20. while( (iPosition = expression.indexIn( prehtml , iPosition )) != -1 ) {
    21. QString semi1 = expression.cap( 1 );
    22. notneed.append(QString("width=\"%1\"").arg(semi1));
    23. notneed.append(QString("width='%1'").arg(semi1));
    24. iPosition += expression.matchedLength();
    25. }
    26.  
    27. iPosition = 0;
    28. while( (iPosition = expression2.indexIn( prehtml , iPosition )) != -1 ) {
    29. QString semi2 = expression2.cap( 1 );
    30. notneed.append(QString("style=\"%1\"").arg(semi2));
    31. notneed.append(QString("style='%1'").arg(semi2));
    32. iPosition += expression2.matchedLength();
    33. }
    34.  
    35. iPosition = 0;
    36. while( (iPosition = expression3.indexIn( prehtml , iPosition )) != -1 ) {
    37. QString semi3 = expression3.cap( 1 );
    38. notneed.append(QString("lang=\"%1\"").arg(semi3));
    39. notneed.append(QString("lang='%1'").arg(semi3));
    40. iPosition += expression3.matchedLength();
    41. }
    42.  
    43. iPosition = 0;
    44. while( (iPosition = expression4.indexIn( prehtml , iPosition )) != -1 ) {
    45. QString semi4 = expression4.cap( 1 );
    46. notneed.append(QString("class=\"%1\"").arg(semi4));
    47. notneed.append(QString("class='%1'").arg(semi4));
    48. iPosition += expression4.matchedLength();
    49. }
    50.  
    51. for (int i = 0; i < notneed.size(); ++i) {
    52. const QString fluteremove = notneed.at(i);
    53. prehtml = prehtml.replace(fluteremove,"", Qt::CaseInsensitive );
    54. }
    55.  
    56. return prehtml;
    57. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt regular expressions!!

    You're parsing HTML right? So why don't you use Qt XML processing facilities??? HTML is nothing but specialized XML...
    Current Qt projects : QCodeEdit, RotiDeCode

  6. #6
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt regular expressions!!

    HTML is not XML. See this example:

    wrong nesting of nodes:
    Qt Code:
    1. <div><b>a</div></b>
    To copy to clipboard, switch view to plain text mode 

    not closing nodes (fint with html 4.01)
    Qt Code:
    1. <ul>
    2. <li>one
    3. <li>two
    4. </il>
    5. <br>
    To copy to clipboard, switch view to plain text mode 

    not using quotes on properties:
    Qt Code:
    1. <img src=test.jpg border=0></img>
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt regular expressions!!

    Quote Originally Posted by elcuco View Post
    HTML is not XML. See this example:
    <img src=test.jpg border=0></img>
    [/code]
    Right .... so many format html the best is XHTML .... tidy can handle .....

    qtclass
    https://qt-webdav.svn.sourceforge.ne...T4_doc/qtidy.h
    static lib .... svn co
    https://qt-webdav.svn.sourceforge.ne.../lib_tidy_src/ path...

    if i grab image on regex or on dom xml as xhtml is work all two.....

    But have a look on word or openoffice or other programm... the copy fragment...
    this is horror...

    Wenn i import on copy paste extern text ... evryting go before on tidy .... otherwise i can not find image and link ..... && i remove extern class && table widht .... from A4 or other
    and reformat to new ....


    tidiconfigfile.append("output-xhtml: yes");
    tidiconfigfile.append("clean: yes");
    tidiconfigfile.append("wrap: 550");
    tidiconfigfile.append("indent-spaces: 1");
    tidiconfigfile.append("char-encoding: utf8");
    tidiconfigfile.append("output-encoding: utf8");
    tidiconfigfile.append("wrap-attributes: yes");
    tidiconfigfile.append("doctype: yes");
    tidiconfigfile.append("hide-comments: yes");
    tidiconfigfile.append("numeric-entities: yes");
    tidiconfigfile.append("drop-proprietary-attributes: yes");
    tidiconfigfile.append("word-2000: yes");
    tidiconfigfile.append("bare: yes");
    //////tidiconfigfile.append("show-body-only: yes"); /* only body checks */



    Qt Code:
    1. void QVimedit::insertFromMimeData ( const QMimeData * source )
    2. {
    3.  
    4. ////////////////QTextEdit::insertFromMimeData(source);
    5. if ( source->formats().contains("text/html") ) {
    6. ////////qDebug() << "### incomming paste text/html ";
    7. const QString tidicaches = QString("%2/.qtidy/").arg(QDir::homePath());
    8. QString draghtml = source->html();
    9. /* fwriteutf8(QString fullFileName,QString xml) */
    10. QTidy *tidy = new QTidy(); /* QTidy *tidy; */
    11. tidy->Init(tidicaches); /* tidy cache remove on last event */
    12. const QString xhtmlnew = tidy->TidyExternalHtml(draghtml);
    13. ///////fwriteutf8("copy_in.html",xhtmlnew);
    14. QTextDocumentFragment fragment = QTextDocumentFragment::fromHtml(xhtmlnew);
    15. textCursor().insertFragment(fragment);
    16. emit IncommingHTML(); /* signal to reload image on resource... QTextDocument */
    17. return;
    18. }
    19. }
    20.  
    21. if hasimage ........
    22.  
    23. if url .....
    To copy to clipboard, switch view to plain text mode 
    Last edited by patrik08; 31st March 2007 at 11:25.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt regular expressions!!

    AFAIK Qt uses xhtml.

  9. #9
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt regular expressions!!

    Quote Originally Posted by wysota View Post
    AFAIK Qt uses xhtml.

    <br>
    this here is xhtml ? QTexEdit you can reload 2 or 6 time

    QTextBrowser (on edit modus) or QTexEdit not handle to <br /> ....
    only tidy handle that .... && asci or other tag....

  10. #10
    Join Date
    Jan 2006
    Location
    Frankfurt, Germany
    Posts
    34
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt regular expressions!!

    if you just want to use the captured groups in a replace, you can also use QString::replace ( const QRegExp & rx, const QString & after ). After can have '\\1', '\\2', etc items, which will be replaced with the corresponding capture groups.

Similar Threads

  1. Regular Expression for QDate [YYYY/MM/DD] format
    By bera82 in forum Qt Programming
    Replies: 6
    Last Post: 3rd August 2019, 09:40
  2. Using QGLWidget paint engine to draw regular widgtes?
    By high_flyer in forum Qt Programming
    Replies: 11
    Last Post: 9th October 2006, 12:06
  3. Qt 4.1 - compile regular DLL statically
    By Elder Orb in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 09:28

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.