Results 1 to 12 of 12

Thread: qstring

  1. #1
    Join Date
    Aug 2012
    Posts
    19
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default qstring

    Hi
    qString str=“Hi my name is Dave, I am using Qt to do my project.”

    Can I use some thing like * or ?
    str2="hi my name is ?, I am using ? to do my project."

    if I compare str2 with str, It should give me Dave and Qt. Something like string Template match.
    Please guide.
    Thank you

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

    Default Re: qstring

    See QRegExp
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2012
    Posts
    19
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qstring

    Qt Code:
    1. QString str="my name is dave";
    2. QRegExp rx("my name is *");
    3. rx.setPatternSyntax(QRegExp::Wildcard);
    4. int pos = 0;
    5. while ((pos = rx.indexIn(str, pos)) != -1) {
    6. qDebug()<< rx.capturedTexts();
    7.  
    8. qDebug() << rx.cap(1);
    9. pos += rx.matchedLength();
    10. }
    To copy to clipboard, switch view to plain text mode 

    This is not working...please help...thank you

  4. #4
    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: qstring

    In order to capture parts of the matched text you need to mark the parts you want in the expression with capturing parentheses. Capturing parentheses are not part of the QRegExp::Wildcard matching scheme, you need to use the default regular expression mode.

    See Capturing Text in the friendly manual.

  5. #5
    Join Date
    Aug 2012
    Posts
    19
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qstring

    Thanks Chris...
    I removed rx.setPatternSyntax(QRegExp::Wildcard)...
    and modified as below...still I am unable to get my required answer that is Dave.
    Qt Code:
    1. QRegExp rx("my name is ((s*))");
    To copy to clipboard, switch view to plain text mode 

    I Even tried //s*. w*...
    Not getting...Regex is too hard.

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

    Default Re: qstring

    ((s*)) means "any number of "s" characters". Is that really what you want?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    May 2012
    Posts
    33
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qstring

    Qt Code:
    1. QString str="my name is dave. I am learning QT";
    2. QRegExp rx("my name is (\\w*). I am learning (\\w*)");
    To copy to clipboard, switch view to plain text mode 

    working fine....
    I am able to extract dave and Qt

  8. #8
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qstring

    You need escape characters before special characters in regexp, or they'll be treated as plain characters.
    Don't forget to escape escape character in code
    For your example:
    Qt Code:
    1. QString str="my name is dave";
    2. QRegExp rx("my name is (\\w+)");
    3. int pos = 0;
    4.  
    5. while ((pos = rx.indexIn(str, pos)) != -1) {
    6. qDebug() << rx.capturedTexts();
    7. qDebug() << rx.cap(1);
    8.  
    9. pos += rx.matchedLength();
    10. };
    To copy to clipboard, switch view to plain text mode 
    Also there's site that I use when trying to come up with particular regexp, hope you find it useful:
    http://gskinner.com/RegExr/

  9. #9
    Join Date
    Aug 2012
    Posts
    19
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qstring

    the string read from the file is below
    reject tcp any any -> any any (content:"twitter.com";msg:"TWITTER1 BEING ACESSED";sid:41325;rev:001
    Qt Code:
    1. QRegExp re("(\\w*) tcp (\\w*) any -> any any \\(content:((\"([A-Za-z0-9_\\./\\-$\\s]*)\"));msg:((\"([A-Za-z0-9_\\./\\-$\\s]*)\"));sid:(\\w*);");
    2.  
    3. while((pos=re.indexIn(str, pos))!=-1){
    4.  
    5. list << re.cap(1);//gives first field from string
    6. list << re.cap(2);//gives 3rd field
    7. list << re.cap(3);//gives content
    8. list << re.cap(6);//gives msg
    9. list << re.cap(9);//gives sid
    10.  
    11. pos+=re.matchedLength();
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 
    The above regex gives me reject, any, twitter.com, twitter being accessed, 41325
    but for that i have to choose 1,2,3,6,9 fields out of the re.cap coz the line has multiple line of quotes.

    but the same doesn't work for the below string
    alert tcp 192.168.1.9 any -> any any (content:"www.gmail.com";msg:"gmail being ACCESSED";sid:41330;rev:001


    please help

  10. #10
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qstring

    I suggest you used a number of simple regexps instead of one complex one trying to capture everything.
    For example your string is "word word ip word -> word word parenthesized_expr"
    So I would captured it like that (double backslashes omitted for clarity)
    "(\w+)\s+(\w+)\s+([0-9\.])\s+(\w+)\s*->\s*(\w+)\s+(\w+)\s*(\([^\)]*)\))"
    Then take a look at parenthesized_expr (which is in your capture group 7)
    (content:"address";msg:"text";sid:num;rev:num; )
    Then write a couple of regexps, like "content:\"([A-Za-z0-9_\./\-$\s]*)\"" and others.
    Divide and conquer

  11. #11
    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: qstring

    Do you expect "\\w*" to match "192.168.1.9"? You have a few redundant sets of () in the expression that make it uglier than it needs to be.

    Try these (you will have to escape the backslashes in C++):
    Qt Code:
    1. (\S+)\s+tcp\s+(\S+)\s+any\s+->\s+any\s+any\s+\(content:"([^"]+)";msg:"([^"]+)";sid:(\d+)
    2. (\S+)\s+tcp\s+(\S+)\s+any\s+->(?:\s+any){2}\s+\(content:"([^"]+)";msg:"([^"]+)";sid:(\d+)
    3. (\S+)\s+tcp\s+(\S+)[^(]+\(content:"([^"]+)";msg:"([^"]+)";sid:(\d+)
    To copy to clipboard, switch view to plain text mode 
    Move some parentheses if you want the quotes that surround content and msg to be captured. If acceptable for your purposes you could run QString::simplified() over the test string first and remove the need to look for repeated whitespace separators.

  12. #12
    Join Date
    Aug 2012
    Posts
    19
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qstring

    Thanks Cris I realized after posting that...now using this 1..its serving my purpose...but its ugly..I am trying ur suggestions
    Qt Code:
    1. QRegExp re("(\\w*) tcp ([a-zA-Z0-9.]*) any -> any any \\(content:((\"([A-Za-z0-9._:\\./\\-$\\s]*)\"));msg:((\"([A-Za-z0-9_\\./\\-$\\s]*)\"));sid:(\\w*);")
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 3
    Last Post: 27th July 2012, 09:30
  2. Replies: 2
    Last Post: 11th August 2011, 15:42
  3. Replies: 8
    Last Post: 25th November 2010, 11:40
  4. Replies: 4
    Last Post: 1st February 2010, 14:21
  5. Replies: 4
    Last Post: 31st January 2008, 20:44

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.