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. Regular expression QT style
Forum Updated to NodeBB v4.3 + New Features

Regular expression QT style

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 1.5k Views 2 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.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on last edited by
    #1

    Finally found a resource which explains the symbols used in regular repression.
    As a result - here is what I know about using "w" - lower case .
    this code matches a single ASCII letter character
    "(\w)"

    this matches a multiple ASCII letters ( plural ) characters - hence a word
    and also the first word in the a string
    "(\w+)"

    now HOW do I code to match the entire string ? AKA multiple words ?
    would placing "$" SOMEWHERE work ?

    And how do I match LAST ASCII word in the string?

    PS It appears that "w+" will match the string terminated as " line" .
    Hence a text file has "lines" and finding a last word in the file takes few additional lines of code.
    I have not been able to duplicate this .

    JonBJ 1 Reply Last reply
    0
    • A Anonymous_Banned275

      Finally found a resource which explains the symbols used in regular repression.
      As a result - here is what I know about using "w" - lower case .
      this code matches a single ASCII letter character
      "(\w)"

      this matches a multiple ASCII letters ( plural ) characters - hence a word
      and also the first word in the a string
      "(\w+)"

      now HOW do I code to match the entire string ? AKA multiple words ?
      would placing "$" SOMEWHERE work ?

      And how do I match LAST ASCII word in the string?

      PS It appears that "w+" will match the string terminated as " line" .
      Hence a text file has "lines" and finding a last word in the file takes few additional lines of code.
      I have not been able to duplicate this .

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @AnneRanch said in Regular expression QT style:

      now HOW do I code to match the entire string ?

      .* matches an entire string.

      AKA multiple words ?

      If you mean you want to capture words something like ((\w+)\s*)* or ((\w+)\W*)*

      A 1 Reply Last reply
      0
      • JonBJ JonB

        @AnneRanch said in Regular expression QT style:

        now HOW do I code to match the entire string ?

        .* matches an entire string.

        AKA multiple words ?

        If you mean you want to capture words something like ((\w+)\s*)* or ((\w+)\W*)*

        A Offline
        A Offline
        Anonymous_Banned275
        wrote on last edited by
        #3

        @JonB Thanks, I got it sort of working. The problem is the "source' is not formatted in nice lines ...
        I'll try to "split" it by "\n" or just counting the characters.
        This is all OK , but what I really need to detect the position of the last character in source and analyze / read next characters.
        I am using "text changed" signal and it always reads the entire editText - I need to read only AFTER the end of the original text.
        Maybe there is another way to 'skin the cat '...

        JoeCFDJ mrjjM 2 Replies Last reply
        0
        • A Offline
          A Offline
          Anonymous_Banned275
          wrote on last edited by
          #4

          I am still not sure what expression to use to retrieve LAST word from the QString.
          The doc is helping, but it would help ,ore if complex repression have better or some verbal description.

          JonBJ 1 Reply Last reply
          0
          • A Anonymous_Banned275

            I am still not sure what expression to use to retrieve LAST word from the QString.
            The doc is helping, but it would help ,ore if complex repression have better or some verbal description.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @AnneRanch
            Depending, you might find (\w+)\W*$ works for you (e.g. try it at https://regex101.com/).

            1 Reply Last reply
            0
            • C Offline
              C Offline
              candy76041820
              wrote on last edited by candy76041820
              #6

              https://regex101.com/r/8zkvRF/1

              QRegularExpression rx(R"(^\W*(\w+\W+)*(?<LastWord>\w+)\W*$)", QRegularExpression::DontCaptureOption);
              QString str="Capturing last word in a line.";
              qDebug()<<rx.match(str).captured("LastWord");
              

              Or, just capture EVERY word and use the last one in match.capturedTexts(), if there isn't a reason to have regex do all the work.

              1 Reply Last reply
              0
              • A Anonymous_Banned275

                @JonB Thanks, I got it sort of working. The problem is the "source' is not formatted in nice lines ...
                I'll try to "split" it by "\n" or just counting the characters.
                This is all OK , but what I really need to detect the position of the last character in source and analyze / read next characters.
                I am using "text changed" signal and it always reads the entire editText - I need to read only AFTER the end of the original text.
                Maybe there is another way to 'skin the cat '...

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by JoeCFD
                #7

                @AnneRanch QString::simplified() to get rid of all "\n" and internal whitespaces if needed.
                https://doc.qt.io/qt-5/qstring.html#simplified

                1 Reply Last reply
                0
                • A Anonymous_Banned275

                  @JonB Thanks, I got it sort of working. The problem is the "source' is not formatted in nice lines ...
                  I'll try to "split" it by "\n" or just counting the characters.
                  This is all OK , but what I really need to detect the position of the last character in source and analyze / read next characters.
                  I am using "text changed" signal and it always reads the entire editText - I need to read only AFTER the end of the original text.
                  Maybe there is another way to 'skin the cat '...

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @AnneRanch
                  Hi
                  Just as a side note.
                  I find you extra brave to try regular expressions or we could call them regular Depression 😜 as
                  the syntax is not what I consider intuitive but it's mostly due to the fact I used them rarely and hence
                  kind of forget about flow and usage.

                  On that note, as a adventure, I tried out
                  https://github.com/VerbalExpressions/CppVerbalExpressions

                  which allows me to write like. (from site)

                  verex expr = verex()
                              .search_one_line()
                              .start_of_line()
                              .then( "http" )
                              .maybe( "s" )
                              .then( "://" )
                              .maybe( "www." )
                              .anything_but( " " )
                              .end_of_line();
                  

                  which enabled me to write some tool code with less fuss getting the syntax right

                  In this case, the same would be 
                  ^(?:http)(?:s)?(?:://)(?:www.)?(?:[^ ]*)$
                  

                  and call me stupid, but (for me) the first syntax just in another league of expressions 🤷‍♂️
                  ( in your use case, it would be silly use to an external lib, just for this task, goes without saying ;) )

                  A 1 Reply Last reply
                  1
                  • mrjjM mrjj

                    @AnneRanch
                    Hi
                    Just as a side note.
                    I find you extra brave to try regular expressions or we could call them regular Depression 😜 as
                    the syntax is not what I consider intuitive but it's mostly due to the fact I used them rarely and hence
                    kind of forget about flow and usage.

                    On that note, as a adventure, I tried out
                    https://github.com/VerbalExpressions/CppVerbalExpressions

                    which allows me to write like. (from site)

                    verex expr = verex()
                                .search_one_line()
                                .start_of_line()
                                .then( "http" )
                                .maybe( "s" )
                                .then( "://" )
                                .maybe( "www." )
                                .anything_but( " " )
                                .end_of_line();
                    

                    which enabled me to write some tool code with less fuss getting the syntax right

                    In this case, the same would be 
                    ^(?:http)(?:s)?(?:://)(?:www.)?(?:[^ ]*)$
                    

                    and call me stupid, but (for me) the first syntax just in another league of expressions 🤷‍♂️
                    ( in your use case, it would be silly use to an external lib, just for this task, goes without saying ;) )

                    A Offline
                    A Offline
                    Anonymous_Banned275
                    wrote on last edited by
                    #9

                    @mrjj Part of the problem is - lack of easy access to symbols ( QT has two 'reg expressions classes and only one has such documentation ) and practically no verbose explanation HOW the expression is interpreted. When I started I received plenty of " do this ..." but no explanation WHY "do this". Such concept is OK if one does not care WHY it works and just blindly "follow the instruction ". That is not how I learn. The main irony of all of this - all I wanted to detect an add to a string - AKA last word entered by user....Since the user is dynamically entering the "word" I can keep analyzing the partial addition or just wait for "Enter" - '\n" - but my string already contains few "\n" ...
                    But I am getting off the subject ...

                    C 1 Reply Last reply
                    0
                    • A Anonymous_Banned275

                      @mrjj Part of the problem is - lack of easy access to symbols ( QT has two 'reg expressions classes and only one has such documentation ) and practically no verbose explanation HOW the expression is interpreted. When I started I received plenty of " do this ..." but no explanation WHY "do this". Such concept is OK if one does not care WHY it works and just blindly "follow the instruction ". That is not how I learn. The main irony of all of this - all I wanted to detect an add to a string - AKA last word entered by user....Since the user is dynamically entering the "word" I can keep analyzing the partial addition or just wait for "Enter" - '\n" - but my string already contains few "\n" ...
                      But I am getting off the subject ...

                      C Offline
                      C Offline
                      candy76041820
                      wrote on last edited by
                      #10

                      @AnneRanch For regex, the reason you don't find explanation is that they can be quite lengthy. Taking some compiler principle lessons to have a clearer understanding is very helpful.
                      As for the scenario here? If real-time responses aren't required, wait for Enter, ui.textBox->text()->split('\n'), then run the regex against each of the lines (w/out the multiline option). Again, no reason to have regex do all the work.
                      Or even, for each non-empty line, find the last index of whitespace as X (-1 if not found), the last index of 0~9/A~Z/a~z as Y (line.length()-1 if not found), then line.substr(X to Y, inclusive) is what you need. No regex at all.

                      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