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. Need help with regular expressions

Need help with regular expressions

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 2.2k 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.
  • QT-static-prgmQ Offline
    QT-static-prgmQ Offline
    QT-static-prgm
    wrote on last edited by A Former User
    #1

    Hi,

    i need your help with my regular expression.

    QString str1 = "[*cspacer]Hallo";
    QString str2 = "[cspacer]Welt";
    QString str3 = "[*spacer7]!!";
    

    That are just examples about how my strings can look like. What they have all together is [spacer] and i want to remove them, but nothing i try works.
    Example1 nothing changes:
    qDebug() << str1.remove(QRegExp("[\x005b*\x005d]")) << ...;

    Example2 the [ is ngone:
    qDebug() << str1.remove(QRegExp("[\x005b]")) << ...;

    Example3 spacer is removed:
    qDebug() << str1.remove(QRegExp("[spacer]")) << ...;

    But Example4 nothing changes:
    qDebug() << str1.remove(QRegExp("[\x005b\x005d]")) << ...;

    i could add a lot of combination i tried, but nothing does what i want to. So i hope someone can show me the correct regular expression

    K A 2 Replies Last reply
    0
    • QT-static-prgmQ QT-static-prgm

      Hi,

      i need your help with my regular expression.

      QString str1 = "[*cspacer]Hallo";
      QString str2 = "[cspacer]Welt";
      QString str3 = "[*spacer7]!!";
      

      That are just examples about how my strings can look like. What they have all together is [spacer] and i want to remove them, but nothing i try works.
      Example1 nothing changes:
      qDebug() << str1.remove(QRegExp("[\x005b*\x005d]")) << ...;

      Example2 the [ is ngone:
      qDebug() << str1.remove(QRegExp("[\x005b]")) << ...;

      Example3 spacer is removed:
      qDebug() << str1.remove(QRegExp("[spacer]")) << ...;

      But Example4 nothing changes:
      qDebug() << str1.remove(QRegExp("[\x005b\x005d]")) << ...;

      i could add a lot of combination i tried, but nothing does what i want to. So i hope someone can show me the correct regular expression

      K Offline
      K Offline
      koahnig
      wrote on last edited by koahnig
      #2

      @QT-static-prgm

      You need to put double backslashes.

      If you like to experiment with QRegularExpression you may want to compile the QRegularExpression example. I am still using the example of the older version for QRegExp.

      Here is the string tested for QRegExp

      "[\[][*|c]{0,2}spacer[\d]{0,1}[\]]{1,1}"

      The string above should match
      [spacer]
      [**spacer]
      [cspacer]
      [*cspacer7]

      [\[] -> matches first bracket
      [*|c]{0,2} -> matches '*' or 'c' , occurrence of eithe rcharacter is 0 up to 2
      spacer -> obviously matches "spacer"
      [\d]{0,1} -> matches one digit, but it is not requried
      [\]]{1,1} -> matches right bracket

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      3
      • QT-static-prgmQ QT-static-prgm

        Hi,

        i need your help with my regular expression.

        QString str1 = "[*cspacer]Hallo";
        QString str2 = "[cspacer]Welt";
        QString str3 = "[*spacer7]!!";
        

        That are just examples about how my strings can look like. What they have all together is [spacer] and i want to remove them, but nothing i try works.
        Example1 nothing changes:
        qDebug() << str1.remove(QRegExp("[\x005b*\x005d]")) << ...;

        Example2 the [ is ngone:
        qDebug() << str1.remove(QRegExp("[\x005b]")) << ...;

        Example3 spacer is removed:
        qDebug() << str1.remove(QRegExp("[spacer]")) << ...;

        But Example4 nothing changes:
        qDebug() << str1.remove(QRegExp("[\x005b\x005d]")) << ...;

        i could add a lot of combination i tried, but nothing does what i want to. So i hope someone can show me the correct regular expression

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

        @QT-static-prgm Check out regexr.com to help you figure out your regular expressions. That's what I do.

        My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

        1 Reply Last reply
        2
        • QT-static-prgmQ Offline
          QT-static-prgmQ Offline
          QT-static-prgm
          wrote on last edited by QT-static-prgm
          #4

          @koahnig it's not exactly what i'm looking for. those strings were just examples.

          In the string is somewhere a
          "[" optional followed by one or more symbols (any symbols, so it can be letters, numbers,...) then there is the word spacer followed again by zero, or more symbols (again any symbols) finally there an "]"

          So it could be for example:
          [kspacer45]==[cool text]==

          and i want to remove the [kspacer45] part.
          In pesudocode:

          if (string contains "[substring]")
              if(substring contains spacer)
                  remove [substring]
          
          

          btw what is the differences between QRegExpr and QRegularExpression??

          ==EDIT==

          Ok this: QRegularExpression("\\[\\*cspacer\\]") works fine for the first example [*cspacer]Hallo

          But how can i represent ANY symbol. i tried \. \? \* But nothing works.
          And how can i say a symbol can be there one or multiple times??

          FlotisableF K 2 Replies Last reply
          0
          • QT-static-prgmQ QT-static-prgm

            @koahnig it's not exactly what i'm looking for. those strings were just examples.

            In the string is somewhere a
            "[" optional followed by one or more symbols (any symbols, so it can be letters, numbers,...) then there is the word spacer followed again by zero, or more symbols (again any symbols) finally there an "]"

            So it could be for example:
            [kspacer45]==[cool text]==

            and i want to remove the [kspacer45] part.
            In pesudocode:

            if (string contains "[substring]")
                if(substring contains spacer)
                    remove [substring]
            
            

            btw what is the differences between QRegExpr and QRegularExpression??

            ==EDIT==

            Ok this: QRegularExpression("\\[\\*cspacer\\]") works fine for the first example [*cspacer]Hallo

            But how can i represent ANY symbol. i tried \. \? \* But nothing works.
            And how can i say a symbol can be there one or multiple times??

            FlotisableF Offline
            FlotisableF Offline
            Flotisable
            wrote on last edited by Flotisable
            #5

            @QT-static-prgm
            if you want to match any character, use .
            if you want to match multiple times, use *( any times ) , + ( at least one time ) or {}( ex. { 2 , 5 } means 2 to 5 times )
            for your case, I think \[.*spacer.*\] may work

            I think you should read some document about regular expression, QRegularExpression official document have some recommended reference, or the website @ambershark had said

            for the difference between QRegExp and QRegularExpression, I think you can read notes for QRegExp users

            by the way, if you use c++11 standard or newer standard, raw string is a good choice for writing regular expression patterns

            1 Reply Last reply
            0
            • QT-static-prgmQ QT-static-prgm

              @koahnig it's not exactly what i'm looking for. those strings were just examples.

              In the string is somewhere a
              "[" optional followed by one or more symbols (any symbols, so it can be letters, numbers,...) then there is the word spacer followed again by zero, or more symbols (again any symbols) finally there an "]"

              So it could be for example:
              [kspacer45]==[cool text]==

              and i want to remove the [kspacer45] part.
              In pesudocode:

              if (string contains "[substring]")
                  if(substring contains spacer)
                      remove [substring]
              
              

              btw what is the differences between QRegExpr and QRegularExpression??

              ==EDIT==

              Ok this: QRegularExpression("\\[\\*cspacer\\]") works fine for the first example [*cspacer]Hallo

              But how can i represent ANY symbol. i tried \. \? \* But nothing works.
              And how can i say a symbol can be there one or multiple times??

              K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

              @QT-static-prgm

              I simply tried to match some of the examples you gave. For more accuracy a crytal ball would be required.

              I read the new explanation like you allow anything starting with a '[' and ending with a ']' and it shall have space in between.

              That would be another string example : "\[.{0,}spacer.{0,}\]"

              \\[             -> matches one left bracket
              .{0,}           -> matches anything from 0 to inifinite occurance
              spacer      -> matches 'spacer' only
              .{0,}           -> matches anything from 0 to inifinite occurance
              \\]             -> matches one right bracket
              

              This matches for instance with one spacer in there:
              [3435323 we got some here some text 52353453423424324spacer45this is a really stupid text""ç"%%""*"*ç"ç"*ç]

              but also with any indefinite number of spacers (here three):
              [3435323 we got some here some text with spacer 52353453423424324spacer45this is a really stupid text with another spacer""ç"%%""*"*ç"ç"*ç]

              This is still QRegExp tested, because of the example code compiled on my machine.

              Once again, I think it is beneficial for you to compile the Qt example code for QRegularExpression and play a little around. Also the webpage suggested by @ambershark is an option as many others in the net.

              Personally, I like also the description and introduction given in QRegExp. It helped me to understand the basics of regular expressions. However, any other page might work best for you.

              I guess you can also write a regular expression ensuring that the word 'spacer' is allowed only once and not multiple times as in the moment. However, I never got as far yet.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              1
              • QT-static-prgmQ Offline
                QT-static-prgmQ Offline
                QT-static-prgm
                wrote on last edited by
                #7

                all right i got it working now :D

                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