Important: Please read the Qt Code of Conduct -

Need to search tag made of wildcards with regexp

  • I have log file containing [] brackets.
    I need to find only those lines in log, that contains specific tag (eg. find only [card]).
    Tags are not being closed, they just indicate what does text afterwards (untill next tag) mean, but that doesn't matter. I mention it only, so expression will have simpler form and not seek ending tag.

    I have found some regular expressions on forums, but there was for () tags.
    I replaced [ with ( and ] with ) just to test if they work because I didn't wanted messing up with wild cards, but still it didn't worked.
    Finnaly I read qregexp article on official qt documentation, but still I cannot figure out formula that I need.

            QString temp_string = log.readLine();   // read line into temporary string
            QRegExp regex("(?<=\\()[^)]*(?=\\))");
    // I have ben told expression above will search for anything in brackets. At that point I didn't care if that's including or excluding brackets, because I wanted to just see any effect at all. 

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    First thing, please use QRegularExpression, QRegExp is to be considered deprecated.

    Then, can you provide some examples of lines that you would like to match ?

  • @Rafiz
    In addition to providing examples here as @SGaist suggests, going to an on-line web site like to play with your attempts is invaluable.

  • when the pattern you are searching for contains symbols that themselves are part of the RE grammar then you must escape them.

    [a-z] searches for any character in the lowercase a-z range
    \[a-z\] searches for the exact text [a-z].

  • Lifetime Qt Champion

    In addition to @JonB suggestion, you also have the QRegularExpression example tool that allows to test your QRegularExpression and get the string you need to use in your code.

  • Thank you all for the answers, there are very proffesional!

    With help of I found some formula close to my problem. I made
    (.*)[searchTag] (.*\n)
    and it does what I needed. Deffinitly adding this tool into my toolbar.

    My input file was in this style :

    [9:47:33][searchTag] juicy data [random tags][more tags] and more data //newline guaranteed at the end of single item
    [9:47:33][searchTag] juicy data [random tags] /no space between some tags/ [more tags] and more data
    [9:47:33][searchTag] juicy data [random tags][more tags] and more data
    [9:47:33][searchTag] juicy data [random tags][more tags] and more data

    and now according to website I have 2 capturing groups, one is getting date (in case I will need it sometime in future it will be better to save it too) and data I want to process to final display table.

    I only have few more questions :

    • Is it "correct" to include new line character in my second capture group? (I found out it works the same way without \n in second capture group)
    • Can I somehow exclude only the middle part of it and not make capture groups?
    • How do I store select groups? I could make negation of whole expression and ".erease" it from string (but I'm almost sure it will be computing power expensive).
      Is there better way to leave in string only those parts which are in search groups?

    I decided to change formula as I will anyway take second round in which I will rewrite useful data to table, so there is no need to erease junk, I will just "not use" it.

    As for
    @SGaist said in Need to search tag made of wildcards with regexp:

    Hi and welcome to devnet,

    First thing, please use QRegularExpression, QRegExp is to be considered deprecated.

    Then, can you provide some examples of lines that you would like to match ?

    I didn't knew about fact that I shouldn't use QRegExp. I just found out it be shorter, so I brainlessly used it :D

    /// Edit : actually I'm reading it line by line, so no \n needed and I only need to use if (myStr.contains([searchTag]))
    /// Edit2 : actually I do not need regular expression anymore... But at least I learned something.

  • EDIT : TL;DR;

    If you try to use regular expression in c++ with no result, try this :

    • Double backslash to escape
    • Capture Item's from expression using () brackets
    • use QRegularExpression instead of QRegExp

    I have problem with square brackets. They don't want to work at

    Here the list of things I tried out and they won't work.

    • substr.contains( QRegularExpression::escape("[") );
    • substr.contains( QRegularExpression("[") );
    • substr.contains( QRegularExpression("\[") );
      !! if I look for substr.contains("\["); it DOES work, but I cannot use regular expression, only QString works
    • substr.contains( QRegularExpression("\\\[") );

    I get always no output! I have substring with [ char, because I output both raw/after lines and I can clearly see that....

    before compile I have

    • warning C4129: '[': unrecognized character escape sequence
      while running I get
    • use of non-standard escape character \[

  • Lifetime Qt Champion


    That is exactly what @Kent-Dorfman said. As the escape character is \, you have to write two backslashes in C++: substr.contains( QRegularExpression("\[") );

    Edit: The dawn forum only shows one backslash, even in code, but I have two of them!

  • Lifetime Qt Champion

    Why do you want to use a full blown regular expression to find if one specific char can be found in your string ? That's a bit overkill.

  • I'm looking for


    but since it doesn't work, I tried to search for


    to see where is problem. And it doesn't work for me no matter how many ( or if any ) backslashes I use

  • Lifetime Qt Champion

    @Rafiz said in Need to search tag made of wildcards with regexp:

    to see where is problem. And it doesn't work for me no matter how many ( or if any ) backslashes I use

    You have to use TWO of them (there is a forum problem that it only shows one, sorry for that)

  • Yeh I wrote that I used two of them (but because of how forum works placing 4 of backshashes was not enough.. I have tried double backslash.

    Unfortunately still it doesn't work for me :/ I'm sitting here about 3 hours only with this one problem and tried many combinations.

    If I write single backshlash I get a bunch of errors, but If I use only two backslashes I have no error but program still doesn't show what I need

    Output only raw lines, so it doesn't find any lines with "["
    Maybe I'm making some mistake and I'm looking in wrong place. I will show more code

    //look at pastebin version, as this version is still corrupted even if I tried escape correctly so it displays well it still doesn't work with this forum x_x
    // ----> <------
    QStringList Lines = temp_string.split('\\n');
                QJsonArray row;
                for ( int i=0 ; i<Lines.size() ; i++ )
                    QJsonValue Jval;
                    QString substr = Lines[i];
                    ui->textEdit->append("raw : "+substr);
                    if (substr.contains("\\\[") )
                    {   // sold[digits]             -> it must be price sold
                        ui->textEdit->append("SSSS "+substr);
                    else if (substr.contains("(bought\[.*?\])"))
                    {   // bought[anything]         -> it must be price bought
                        ui->textEdit->append("BBBB "+substr);

    Part of output (whole thing is long to say at least) :

    raw : [9:47:33][TradePileWorker]
    raw : sold[Brad Guzan]
    raw : player[73]
    raw : C[GkBasic]
    raw : P[GK]
    raw : bought[600]
    raw : sold[950]
    raw :
    raw : [9:47:33][TradePileWorker]
    raw : sold[Mattheus Oliveira]

    Part of input :

    sold[Brad Guzan]
    sold[Mattheus Oliveira]
    sold[Stephane Sparagna]

    I just hope it will display well... In case not here's another coppy of the same code :

    I'm using Qt Creator 4.9.1
    I'm using Qt 5.12.3

  • This post is deleted!

  • Ok, I figured it out.

    1. I had to use QRegularExpression
    2. Double backslash worked (it's also for any other regular expression thing (like \\d) )
    3. I had to capture what I got with normal brackets ()

    My problem was I was testing weather or not I need to capture with brackets, but I tested it when I was in completely wrong wariant of 1. and 2. problems. That's why I wrongly deduced, that I don't need to capture results. (My expression was invalid in the beggining and so it passed everything through) Than I never tried using brackets again and I tried to solve it by manipulating first 2 variants...

  • Lifetime Qt Champion

    There are several things at play here. If looking for just the [ char, you can use substr.contains('[') which is the char overload of contains.
    If you want to use a regular expression, you have to wrap it using QRegularExpession otherwise it's the QString overload that will be used and that one does a "literal search".

Log in to reply