How to use regular expression regex_replace to simulate reading INI files
This article mainly introduces how to use regular expression regex_replace to simulate reading INI files, which is very detailed and has certain reference value. Friends who are interested must finish reading!
The specific code is as follows:
# include "stdio.h" # include # include using namespace std; void Trim (char * str); void lTrim (char * str); void rTrim (char * str) / / the extended function provided by sscanf and regular expression / / sscanf can not really be called a regular expression, because his writing is still inseparable from% / [] to represent the character range, {} to indicate the number of repetitions, ^ to take no, and * to skip. So the parsing of the above url can be written as follows: / char url [] = "dv://192.168.1.253:65001/1/1" / sscanf (url, "% [^: / /]% * c% c%d%*c%d%*c%d% [^:]% * c%d%*c%d%*c%d", protocol, ip, port, chn, type) / / explain / / get the longest string first, but not the string: / /, so protocol= "dv\ 0" / / then skip three characters, (% * c) is actually skip: / then take a string that does not include a string:, so ip = 192.168.1.253, simplified here, IP will do as a string, and do not check / then skip colons to take the port to port, then skip / take the channel number to chn, and then skip / take the stream type to type. / / C language implementation of the above example void test1 () {char url [] = "dv://192.168.1.253:65001/1/1"; char protocol [10]; char ip [17]; int port; int chn; int type; sscanf (url, "% [^: / /]% * c% c%d%*c%d%*c%d% [^:]% * c%d%*c%d%*c%d", protocol, ip, & port, & chn, & type) Printf ("% s,% s,% d,% d,% d\ n", protocol, ip, port, chn, type) } / / read a line string in ini Get: hello world! / / normal string 1:-claim= "hello world!" / / normal string 2: claim= "hello world!" / / normal string 3: claim= "hello world!" / / normal string 4: claim_ = hello world! / / interference string 1: cl-aim =\ "hello world!" / / interference string 2: clai3m = "hello world!\" / / interference string 3: cla_im = \\ "hello world!\" / interference string 4: claim='"hello world!\" / / interference string 5: claim= @ "\ nhello world!" / / interference string 6: claim=L "hello world!" / / unhandled 1: claim [1] = 1 / / unhandled 1: claim [2] = 1 void test2 () {char line [1000] = {0} Char val [1000] = {0}; char key [1000] = {0}; FILE * fp = fopen ("1.txt", "r"); if (NULL = = fp) {printf ("failed to open 1.txt\ n"); return;} while (! feof (fp)) {memset (line, 0, sizeof (line)); fgets (line, sizeof (line)-1, fp) / / contains the printf ("% s", line) of each line; Trim (line); / / extracts the content before the equal sign memset (key, 0, sizeof (key)); / / the format used by sscanf is not a regular expression and cannot be used to represent various white space characters, that is, spaces or\ t,\ n,\ r,\ f sscanf (line, "% [^\ t\ n\ r\ f =]", key) / / sscanf (line, "% * [^ a-zA-Z0-9 percent -]% [^\ t\ n\ r\ f =]", key); printf ("key: [% s]\ n", key); / / extract the content after the equal sign memset (val, 0, sizeof (val)); sscanf (line, "% * [^ =]% * c% [^\ n]", val); / / does not contain the newline character Trim (val) for each line Printf ("val: [% s]\ n", val); / / remove double quotes /. / / insert map / / map [key] = value; / / string to other types / / atoi, atol, atof} printf ("\ n"); fclose (fp);} / / the C++ implementation of template inline T1 parseTo (const T2 t) {static stringstream sstream; T1 r; sstream > r Sstream.clear (); return r;} void test3 () {char val [1000] = {0}; char key [1000] = {0}; ifstream fin ("1.txt"); string line; if (fin) {while (getline (fin, line)) / / line does not include the newline character {cout for each line