Important: Please read the Qt Code of Conduct -

replace( QRegularExpression(".*"), "a:\folder\1-file"))

  • Have this

    replace( QRegularExpression(".*"), "a:\folder\1-file"))

    So how to make '\1' to be just a regular text, and
    not a placeholder for the captured( 1) from
    QRE match group ?

    Any idea.

  • I am not sure that it is what you want but try use double baskslashes to avoid captured by QRE.

    replace( QRegularExpression(".*"), "a:\\folder\\1-file") )

  • @StevenG
    Firstly, your literal string is wrong, this is just C++ 101. To even get something vaguely presentable you need

    replace( QRegularExpression(".*"), "a:\\folder\\1-file"))

    If you don't know the rules for protecting backslashes in C strings you should read up on them.

    Secondly it's not possible to give a correct answer to your question without knowing what the replace() method is. You choose to ask a question without even telling us that. There is no QRegularExpression::replace(), so who knows.

    not a placeholder for the captured( 1) from

    What capture? There is no capture/regular expressioning going on, at least not without know what replace() is....

    I will charitably assume you are talking about the replace() method of QString,

    I agree there is a problem: the docs state only

    For regular expressions containing capturing groups, occurrences of \1, \2, ..., in after are replaced with the string captured by the corresponding capturing group.

    without telling you how you can avoid that. However, they do state "For regular expressions containing capturing groups". Your reg exp, .*, does not contain any capturing groups, so hopefully this will simply not apply and \1 will be treated as literal. So, have you actually tried it (as corrected above) to see what happens?

    Furthermore, reg ex .* should match the whole string. In that case, won't it replace the whole string with a:\folder\1-file? And in that case, what's the point of doing any reg ex replace?!

Log in to reply