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. Problem with regular expression

Problem with regular expression

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 5.9k Views 1 Watching
  • 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.
  • W Offline
    W Offline
    willypuzzle
    wrote on last edited by
    #1

    @
    QRegExp exp("\b([a-gA-G][#bd]?)\b");
    exp.setMinimal(true);
    @

    This expression should match the seven musical notes in a insensitive manner. But it doesn't work because '#' is recognized as boundary character first the '[#bd]' character.
    For example i tried it against 'C#' it match only the 'C' character like '#C#'.
    I tried to not set Minimal and it doesn't work anyway, As I tried QRegExp::QRegExp2 and it doesn't work.
    Is it a bug?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      baysmith
      wrote on last edited by
      #2

      '#' is non-word character and therefore matches it as a word boundary in 'C#'. It matches 'C#' without the word boundaries.

      @
      QRegExp exp("([a-gA-G][#bd]?)")
      @

      Why are you using word boundaries?

      Nokia Certified Qt Specialist.

      1 Reply Last reply
      0
      • W Offline
        W Offline
        willypuzzle
        wrote on last edited by
        #3

        I need to parse web page to find musical chords and '#' is usually used to denote sharp (or diesis) note.
        for example C# is a common sign I can found.

        1 Reply Last reply
        0
        • W Offline
          W Offline
          willypuzzle
          wrote on last edited by
          #4

          My exp has to catch A# or C# only if they are "isolated" from the rest of the content page.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            so you could define whitespaces before / after:

            @
            QRegExp exp("(\s[a-gA-G][#bd]?\s)")
            @

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • W Offline
              W Offline
              willypuzzle
              wrote on last edited by
              #6

              Yes but so RegExp engine catches the spaces too, and in my application this is not good,

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on last edited by
                #7

                Come on, be a bit creative:

                @
                QRegExp exp("\s([a-gA-G][#bd]?)\s")
                @

                moving the breakets would do it, right?

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                1 Reply Last reply
                0
                • W Offline
                  W Offline
                  willypuzzle
                  wrote on last edited by
                  #8

                  Sorry it continues to catch the spaces at the boundary of expression.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #9

                    You don't want the matched text, but the captions. Have a look at "QRegExp::cap() ":http://doc.qt.nokia.com/latest/qregexp.html#cap and the sample usage in the "Capturing Text":http://doc.qt.nokia.com/latest/qregexp.html#capturing-text seciton of the docs:

                    @
                    QRegExp exp("\s([a-gA-G][#bd]?)\s");
                    QString test("You like the chord C# very well!");
                    int pos = 0;
                    while((pos = exp.indexIn(test, pos)) != -1) {
                    qDebug() << "found '" + exp.cap(1) + "'";
                    pos += exp.matchedLength();
                    }
                    @

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • W Offline
                      W Offline
                      willypuzzle
                      wrote on last edited by
                      #10

                      I'm using QRegExp with QTextDocument::find function and QTextCursor,

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        goetz
                        wrote on last edited by
                        #11

                        I doubt that this will be possible with regular expressions (you can non-match the word boundary with a "positive lookahead assertion":http://doc.qt.nokia.com/latest/qregexp.html#assertions, but unfortunately there is no similar "look back" assertion. There is an jira issue open with a suggestion for this ("QTBUGS-2371":http://bugreports.qt.nokia.com/browse/QTBUG-2371), you can vote for it.

                        http://www.catb.org/~esr/faqs/smart-questions.html

                        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