Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Creator: How do I retain a regexp when replacing?



  • I would like to replace instances like
    @obj.m_Member = "abc";@
    to something like
    @obj.setMember("abc");@

    However, replace does not quite do what I expect:
    I search using @obj.m_Member = "\w+";@ and get the results I want.
    However, when I replace with @obj.setMember("\w+");@, the \w+ is taken literally, not as the regexp result.

    Anything I can do, or is this feature simply missing?


  • Lifetime Qt Champion

    Hi,

    It's not missing. Without any pun you are getting what you are asking for.

    @obj.m_Member = ("\w+") <- capture what's between the parenthesis@

    @obj.Member(\1) <- use first captured text@

    Should to what you want



  • Good to know. Why does it capture "what's between the parenthesis"? Parenthesis are so common in code it seems like a strange symbol to load with a special meta-meaning.


  • Lifetime Qt Champion

    That's defined by the regexp rules: a set of parenthesis captures what's in between but that's just it's simplest usage


Log in to reply