Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QRegExp : search a pattern that does not contain another pattern

QRegExp : search a pattern that does not contain another pattern

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    floreal797
    wrote on last edited by
    #1

    Hi,

    I am trying to search the following pattern :
    "<b>.+</b>$"

    But this pattern must not contain a "<b>" tag.

    Given the following example:
    abba <b>acba</b> <b>adba</b>

    If I use the previous pattern, the result is :
    <b>acba</b> <b>adba</b>

    But the good result, which I want is :
    <b>adba</b>

    Because I want disallow a <b> tag between the two <b> and </b> border tags.

    So, how can I do that?

    I use QRegExp and I code in c++ language.

    Thank you in advance for your help,

    Floréal.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      One way would be to change the pattern to "<b>.+</b>" and setMinimal(true).
      This would give you 2 results: "<b>acba</b>" and "<b>adba</b>". You can then just take the last one.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Using .+ is a bad idea, essentially you are telling that you want a <b> then everything and anything at list once then a </b> at the end of the line. Looks you are rather looking for <b> at least one word character then </b> end of the line so something like

        @<b>\w+</b>$@

        Beware this is the real regexp not the one escaped for use with QRegExp.

        On a side not you should consider using QRegularExpression if you are using Qt 5

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • F Offline
          F Offline
          floreal797
          wrote on last edited by
          #4

          Thanks for your quick replies. But...

          [quote author="Chris Kawa" date="1418216735"]One way would be to change the pattern to "<b>.+</b>" and setMinimal(true).
          This would give you 2 results: "<b>acba</b>" and "<b>adba</b>". You can then just take the last one.
          [/quote]

          I have try it. I use this code:
          @QString res = "not found";
          QString data = "abba <b>acba</b> <b>adba</b>";
          QRegExp exp;
          exp.setMinimal(true);
          exp.setPattern("<b>.+</b>");
          if (data.contains(exp))
          {
          QStringList list = exp.capturedTexts();
          foreach (QString cap, list) {
          qDebug() << cap;
          }
          }@

          But it give me only one result, and the first, not the last:
          "<b>acba</b>"

          May be something is wrong?

          [quote author="SGaist" date="1418217112"]Hi,

          Using .+ is a bad idea, essentially you are telling that you want a <b> then everything and anything at list once then a </b> at the end of the line. Looks you are rather looking for <b> at least one word character then </b> end of the line so something like

          @<b>\w+</b>$@

          Beware this is the real regexp not the one escaped for use with QRegExp.

          On a side not you should consider using QRegularExpression if you are using Qt 5[/quote]

          I have already tested it, and it works very well. But the string I given here is only an example. In reality, the content between <b> and </b> might not be alphanumerical characters. For example, we can have :
          abcd <b>acba</b> <b>ad i>2 ba</b>

          where we want to find :
          <b>ad i>2 ba</b>

          And here, the solution you give does not work anymore...

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Then use a more specific regexp like

            @<b>[\w\s>]+</b>$@

            Complete it with the special chars you might get

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              bq. But it give me only one result, and the first, not the last

              capturedTexts() returns a list strings captured in a capture clause ( and ). You don't have such. The first element in that list is always the entire matched string so its of no use to you. Either surround the part that you want to capture with () or iterate over matches using indexIn().

              Or go with the suggestion SGaist gave.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved