Important: Please read the Qt Code of Conduct -

Strings fields *,?

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

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

    This should give me Dave and Qt. Something like string Template match.
    Please guide.
    Thank you

  • Hi! If i understand you correctly, you can use "QRegExp":

  • Thanks qxoz..But in that reference page there is only regarding file matching like *.html etc. how to pass with my string and get values.
    Can u please provide some snippet or any other reference.

  • Moderators

    It does not matter whether a string contains a filename or a sentence, the principle is the same.

    There are plenty of regular expression tutorials around with examples and all. Just use google. RegExps are something that should be in every programmers toolbox, so learning about them will not be a waste of time!

  • Hi all...I used this..but not working...tried some others like * , \W* etc
    @QString str="my name is dave";
    QRegExp rx("my name is [^ ]*");
    int pos = 0;
    while ((pos = rx.indexIn(str, pos)) != -1) {
    qDebug()<< rx.capturedTexts();

    qDebug() << rx.cap(1);
    pos += rx.matchedLength();
    Whats wrong...Please help

  • Lifetime Qt Champion


    AFAIK, you don't use a capturing RegExp, you have to use parenthesis to capture something. Take a look at the QRegExp doc, specially the "capturing text part":

  • the string read from the file is below
    reject tcp any any -> any any (content:"";msg:"TWITTER1 BEING ACESSED";sid:41325;rev:001;)
    QRegExp re("(\w*) tcp (\w*) any -> any any \(content:(("([A-Za-z0-9_\./\-$\s])"));msg:(("([A-Za-z0-9_\./\-$\s])"));sid:(\w*);");

    while((pos=re.indexIn(str, pos))!=-1){

            list << re.cap(1);//gives first field from string
            list << re.cap(2);//gives 3rd field
            list << re.cap(3);//gives content
            list << re.cap(6);//gives msg
            list << re.cap(9);//gives sid

    The above regex gives me reject, any,, 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 any -> any any (content:"";msg:"gmail being ACCESSED";sid:41330;rev:001;)

    please help

Log in to reply