Need help with regular expressions



  • 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


  • Moderators

    @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


  • Moderators

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



  • @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??



  • @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


  • Moderators

    @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.



  • all right i got it working now :D


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.