Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Kind of a brain freeze question (I'm planing a much anticipated vacation).

I have a string like :

$(F("xxx", "zzz") )

I need to extract xxx and zzz.

I could do it manually, but I'm certain there is a better way.

regular expression ? or something else ?

Contrary to other, this is not really urgent, and hints and tips would be much appreciated instead of a direct answer (but don't let that stop you! :-))

Thanks.

Max.
Posted

I'd use a Regular Expression. \$\(F\(\"(?'X'[^\"]*?)\",\s*\"(?'Z'[^\"]*?)\"\)\s*\)
But you may want more flexibility.


And we have a Regular Expressions forum here as well.
 
Share this answer
 
v2
Comments
Andreas Gieriet 6-Mar-13 23:58pm    
Since C++11, there are also verbatim strings (called "raw string literals").
E.g., the regex could be written as:
string pattern = R"rx(^\$\(F\s*\(\s*("(?:\.|[^"])*")\s*,\s*("(?:\.|[^"])*")\s*\)\s*\)\s*$)rx";
With this, you do not need to double your backslashes in the pattern string.
Cheers
Andi
www.txt2re.com can be used to generate a Regular Expression and provide the code to execute the Regular Expression match.

This is one that I created using txt2re to extract xxx (word1) and zzz (word2). Depending on the variability in the format of your source string, you may or may not have to generate a different Regular Expression.
// URL that generated this code:
// http://www.txt2re.com/index-c++.php3?s=$(F("xxx",%20"zzz")%20)&-24&-25&-28&30&-26&-20&4&-21&-29&-22&5&-23&-27

#include <stdlib.h>
#include <string>
#include <iostream>
#include <pme.h>

int main()
{
  std::string txt="$(F(\"xxx\", \"zzz\") )";

  std::string re1="(\\$)";	// Any Single Character 1
  std::string re2="(\\()";	// Any Single Character 2
  std::string re3="(.)";	// Any Single Character 3
  std::string re4="(\\()";	// Any Single Character 4
  std::string re5="(\")";	// Any Single Character 5
  std::string re6="((?:[a-z][a-z]+))";	// Word 1
  std::string re7="(\")";	// Any Single Character 6
  std::string re8="(,)";	// Any Single Character 7
  std::string re9=".*?";	// Non-greedy match on filler
  std::string re10="(\")";	// Any Single Character 8
  std::string re11="((?:[a-z][a-z]+))";	// Word 2
  std::string re12="(\")";	// Any Single Character 9
  std::string re13="(\\))";	// Any Single Character 10
  std::string re14=".*?";	// Non-greedy match on filler
  std::string re15="(\\))";	// Any Single Character 11

  PME re(re1+re2+re3+re4+re5+re6+re7+re8+re9+re10+re11+re12+re13+re14+re15,"gims");
  int n;
  if ((n=re.match(txt))>0)
  {
      std::string c1=re[1].c_str();
      std::string c2=re[2].c_str();
      std::string c3=re[3].c_str();
      std::string c4=re[4].c_str();
      std::string c5=re[5].c_str();
      std::string word1=re[6].c_str();
      std::string c6=re[7].c_str();
      std::string c7=re[8].c_str();
      std::string c8=re[9].c_str();
      std::string word2=re[10].c_str();
      std::string c9=re[11].c_str();
      std::string c10=re[12].c_str();
      std::string c11=re[13].c_str();
      std::cout << "("<<c1<<")"<<"("<<c2<<")"<<"("<<c3<<")"<<"("<<c4<<")"<<"("<<c5<<")"<<"("<<word1<<")"<<"("<<c6<<")"<<"("<<c7<<")"<<"("<<c8<<")"<<"("<<word2<<")"<<"("<<c9<<")"<<"("<<c10<<")"<<"("<<c11<<")"<< std::endl;
  }
}

//-----
// C++ does not provide a regular expression feature as standard.
//
// To run this code you will need to first download and install
// the PCRE library from http://www.pcre.org/ and 
// the PME library from ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/Contrib/
// 
// Note that on Linux systems PCRE is often already installed in /usr/lib/libpcre* or /usr/local/lib/libpcre*. 
//
// Compile and run on Unix using 
// # c++ x.cpp -lpme -lpcre 
// # ./a.out
// </pme.h></iostream></string></stdlib.h>
 
Share this answer
 
Comments
Andreas Gieriet 6-Mar-13 23:49pm    
C++11 has regex support. See Regular Expressions <regex>. The syntax of the regex is ECMAScript regular expressions pattern syntax.
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900