Results 1 to 11 of 11

Thread: QReqExp for (v1=vlaue1 v2 = value2 v3= value3 )

  1. #1
    Join Date
    Jun 2012
    Posts
    173
    Thanks
    48
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QReqExp for (v1=vlaue1 v2 = value2 v3= value3 )

    Hello Guys,

    I need help for ReqExp for parse one line e.x

    string {any number of spaces} = String {any unmber of spaces} String = string)
    just like

    QString line = "v1=vlaue1 v2 = value2 v3= value3"

    I need to split this QString to get QStringList of pairs of the variable and its value such as :

    QStringList tmp;
    tmp = line.split(ReqExp(....) )

    /*the resulted tmp will be like :
    tmp[0]= "v1=vlaue1"
    tmp[1]= "v2 = value2"
    tmp[2]= "v3= value3"
    */

    so please could you help me with the ReqExp so i can use it with the split function.

    PS: the line could has only one pair(v1=vlaue1) or maybe more (v1=vlaue1 .................vn=vlauen).


    Thank you in advance.


    Added after 50 minutes:


    I though it could be something like this

    QRegExp("//s*//w*//s*=//s*//w*")

    but this does not work with me, im sure im not doing the expression correct any help will be appreciated
    Last edited by jesse_mark; 20th November 2012 at 20:19.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QReqExp for (v1=vlaue1 v2 = value2 v3= value3 )

    how about just use split with separators = and space??

    If you're gonna use regex, at least use the correct slash

    Oh yea, if you use split(regex), then the bit you capture with regex will get thrown away! Same as if you did split(",") - you don't want to keep the ",", you want to split on it!

    Qt Code:
    1. QString line = " v1 = 1 v2 = 2 v3=three ";
    2.  
    3. QString line2 = line.replace("=", " ");
    4. QStringList qsl = line.split(" ", QString::SkipEmptyParts);
    To copy to clipboard, switch view to plain text mode 


    If you want to use regex, then indexIn(), and matchedLength() would need to be used.
    Qt Code:
    1. QRegExp re("\\s*\\w*\\s*=\\s*\\w*\\s*");
    2. QString line = " v1 = 1 v2 = 2 v3=three ";
    3. int i0 = re.indexIn(line);
    4. int i1 = re.matchedLength();
    5. int i2 = re.lastIndexIn(line);
    6. int i3 = re.matchedLength();
    To copy to clipboard, switch view to plain text mode 
    Last edited by amleto; 20th November 2012 at 21:31.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. The following user says thank you to amleto for this useful post:

    jesse_mark (20th November 2012)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QReqExp for (v1=vlaue1 v2 = value2 v3= value3 )

    The pattern used by split() is supposed to match the delimiters, not the bits you want to keep. You would need the pattern to match any number of spaces not preceded or followed by an '='. I think this would be tricky without look-behind assertions (not supported in QRegExp).

    You could use amleto's suggestion to get a list of "words" and then reconstitute the pairs.

    Or, don't try to split the string; deliberately extract the matching parts (straight from the docs):
    Qt Code:
    1. QString line = "v1=vlaue1 v2 = value2 v3= value3";
    2.  
    3. QRegExp re("\\w+\\s*=\\s*\\w+");
    4. QStringList result;
    5. int pos = 0;
    6. while ((pos = re.indexIn(line, pos)) != -1) {
    7. result << re.cap(0);
    8. pos += re.matchedLength();
    9. }
    10. qDebug() << result;
    11. // ("v1=vlaue1", "v2 = value2", "v3= value3")
    To copy to clipboard, switch view to plain text mode 
    This assumes the name and value cannot themselves contain spaces or '='.

  5. The following user says thank you to ChrisW67 for this useful post:

    jesse_mark (20th November 2012)

  6. #4
    Join Date
    Jun 2012
    Posts
    173
    Thanks
    48
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QReqExp for (v1=vlaue1 v2 = value2 v3= value3 )

    Thank you so much for the the idea and helping with code.

    which way do you recommend me to use ??

  7. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QReqExp for (v1=vlaue1 v2 = value2 v3= value3 )

    personally, I don't see the benefit from using regex here so I would avoid it.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  8. The following user says thank you to amleto for this useful post:

    jesse_mark (21st November 2012)

  9. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QReqExp for (v1=vlaue1 v2 = value2 v3= value3 )

    Quote Originally Posted by jesse_mark View Post
    which way do you recommend me to use ??
    As always, one that works for you. My option gives you what you asked for (and handles any number of name/value pairs) but I do not know what you are ultimately going to do with the elements in the string list. If you are only going to later split them on the '=' then the replace/split approach in Amleto's post gets you there faster. If you must preserve the uneven spacing around '=' then you cannot use replace/split approach.

  10. The following user says thank you to ChrisW67 for this useful post:

    jesse_mark (21st November 2012)

  11. #7
    Join Date
    Jun 2012
    Posts
    173
    Thanks
    48
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QReqExp for (v1=vlaue1 v2 = value2 v3= value3 )

    Thank you so much for your suggestions.

    I used both the regexp and the replace/split.
    I used to the regexp just to make sure the line has the format that i expect it and the replace/split to do the parsing.

  12. #8
    Join Date
    Jun 2012
    Posts
    173
    Thanks
    48
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QReqExp for (v1=vlaue1 v2 = value2 v3= value3 )

    I faced an issue now, when using both ways.

    f.e. is
    line = "V1= V2=x V3 = y"
    if one of the variables were missing its value, the both ways gave me wrong result.

  13. #9
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QReqExp for (v1=vlaue1 v2 = value2 v3= value3 )

    surely that does not count as a 'valid' line?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  14. #10
    Join Date
    Jun 2012
    Posts
    173
    Thanks
    48
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QReqExp for (v1=vlaue1 v2 = value2 v3= value3 )

    Yeah, but how can I validate it ??

  15. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QReqExp for (v1=vlaue1 v2 = value2 v3= value3 )

    Quote Originally Posted by jesse_mark View Post
    line = "V1= V2=x V3 = y"
    if one of the variables were missing its value, the both ways gave me wrong result.
    Why of course, Sir, here's what you specified:
    string {any number of spaces} = String {any unmber of spaces} String = string)
    with no mention of the possibility of empty strings.

    With your new input how is the expression supposed to know that V2 is the name of a second variable and not the value of the first? You might say, "but it's followed by an equal sign, and that makes it a name" That is not an adequate description because you allow space between the name and the '=' e.g. if the line was:
    Qt Code:
    1. V1= V2 = x V3 = y
    To copy to clipboard, switch view to plain text mode 
    A naïve attempt using my base regexp:
    Qt Code:
    1. QString line = "v1= v2 = value2 v3= value3";
    2. QRegExp re("\\w+\\s*=\\s*\\w*(?!\\s+=)");
    3. // ("v1= v", "2 = value2", "v3= value3")
    To copy to clipboard, switch view to plain text mode 
    fails in new and unexpected ways. This comes closer:
    Qt Code:
    1. QRegExp re("(\\w+\\s*=\\s*\\w*)\\s(?!\\s*=)");
    2. QStringList result;
    3. int pos = 0;
    4. while ((pos = re.indexIn(line, pos)) != -1) {
    5. result << re.cap(1);
    6. pos += re.matchedLength();
    7. }
    8. qDebug() << result;
    9. // ("v1=", "v2 = value2", "v3=")
    To copy to clipboard, switch view to plain text mode 
    but I can almost guarantee that it will fail for some other input.

    Parsing dirty data is not trivial. Often the only way is to pull the text apart and code the rules and exceptions.

Similar Threads

  1. QReqExp for any number of white spaces
    By jesse_mark in forum Newbie
    Replies: 4
    Last Post: 10th November 2012, 06:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.