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

Find a complex substring in another string



  • Hi everybody,
    I have a substring for example:
    43ab??12??56??ef
    ?? are wildcard and the count of ?? is unknown.
    I want to check the main string contains this substring or not.
    The substring is not unique and it changes always.


  • Moderators

    Hi,

    You want to use Regular Expressions. Use this as your pattern: 43ab.*12.*56.*ef



  • @JKSH Hi dear JKSH. Thanks for your reply.
    I know but my substring is unpredictable and I can not use a constant regular expression.
    For example:
    32??
    or
    ??ae8298
    or
    ??????34????
    or
    5798a3??12??dbfe4a93??b2
    or ...


  • Moderators

    @MohammadReza said:

    @JKSH Hi dear JKSH. Thanks for your reply.
    I know but my substring is unpredictable and I can not use a constant regular expression.

    Well, what are the characteristics that are common to all of your substrings? You need to identify this information first, before you can design your search logic.

    How are these substrings generated? What are they used for? What other things do your "main string" contain?



  • @JKSH Thanks.
    By using of RegExp, the problem is solved.
    For other users who read this question:
    My main string is a string of hex (without any space or \x or 0x). I replcae ? in substring by [0-9a-f] and use Qt::CaseInsensitive option:

    regex.replace("?","[0-9a-f]");
    index = mainString.indexOf(QRegExp(regex,Qt::CaseInsensitive));
    

Log in to reply