C language sscanf

sscanf is similar to scanf , both are used for input, except that the latter uses the keyboard ( stdin ) as the input source, and the former uses a fixed string as the input source. It can be extracted with complex string parsing, such as cjson string parsing.

The second parameter can be one or more {%[*] [ width ] [{h | I | I64 | L}]type | ' ' | '\t' | '\n' | non-% symbol}

Note:

1. * can also be used in formats (i.e. %*d and %*s). Adding an asterisk  (*) means skipping this data and not reading it in. (That is, not reading this data into the parameter)

2. {a|b|c} means to choose one of a, b, c, [d] means that it can have d or not.

3. Width represents the reading width.

4. {h | l | I64 | L}: parameter size, usually h represents single-byte size, I represents 2-byte size, L represents 4-byte size (double exception), l64 represents 8-byte size.

5. type: There are many, such as %s, %d and so on.

6. Special: %*[width] [{h | l | I64 | L}]type means that those that meet this condition will be filtered out and no value will be written to the target parameter.

Returns 0 on failure, otherwise returns the number of formatted parameters.

7. If the read string is not separated by spaces, you can use %[].

1. General usage

1

2

3

char buf[512] = ;

sscanf("123456 ""%s", buf);

printf("%s\n", buf);

The result is: 123456

2. Get a string of specified length. As in the following example, take a string with a maximum length of 4 bytes.

1

2

sscanf("123456 ""%4s", buf);

printf("[%s]\n", buf);

The result is: [1234]

3. Get the string up to the specified character. As in the following example, get the string until a space is encountered.

1

2

sscanf("123456 abcdedf""%[^ ]", buf);

printf("%s\n", buf);

The result is: 123456

4. Get a string containing only the specified character set . As in the following example, take a string containing only 1 to 9 and lowercase letters .

1

2

sscanf("123456abcdedfBCDEF""%[1-9a-z]", buf);

printf("%s\n", buf);

The result is: 123456abcdedf

5. Get the string up to the specified character set. As in the following example, get the string until an uppercase letter is encountered.

1

2

sscanf("123456abcdedfBCDEF""%[^A-Z]", buf);

printf("%s\n", buf);

The result is: 123456abcdedf

6. Given a string iios/12DDWDFF@122, get the string between / and @, first filter out "iios/", and then send the non-'@' string to buf

1

2

sscanf("iios/12DDWDFF@122""%*[^/]/%[^@]", buf);

printf("%s\n", buf);

The result is: 12DDWDFF

7. Given a string "hello, world", only "world" is retained . (Note: There is a space after ",")

1

2

sscanf("hello, world""%*s%s", buf);

printf("%s\n", buf);

The result is: world   P.S.  %*s means that the first matching %s is filtered out, that is, hello, is filtered. If there are no spaces, the result is NULL.

8. Other regular expressions

From this experiment we will use regular expressions. The az in the brackets is a regular expression, which can represent any character from a to z.

Replenish:

Usage of %[ ]: %[ ] means to read a character set. If the first character after [ is "^", it means the opposite meaning.

                    The string within [ ] can be composed of 1 or more characters. An empty character set (%[]) is illegal and may

                    leading to unpredictable results. %[^] is also against the rules.

%[az] reads the string between az, and stops if it is not before, such as

             char s[]=”hello, my friend”; // Note: , the comma is between az and az

             sscanf( s, “%[a-z]”, string ) ; // string=hello

%[^az] reads a string that is not between az and stops if it encounters a character between az, such as

             char s[]=”HELLOkitty”; // Note: comma is between az and az

             sscanf( s, “%[^a-z]”, string ) ; // string=HELLO

%*[^=] with an * sign in front means that the variable is not saved. Skip matching strings.

             char s[]=”notepad=1.0.0.1001″ ;

      char szfilename [32] = “” ;

      int i = sscanf( s, “%*[^=]”, szfilename ); // szfilename=NULL, because it was not saved

int i = sscanf( s, “%*[^=]=%s”, szfilename ) ; // szfilename=1.0.0.1001

%[^=] reads the string until it encounters the '=' sign, more characters can be added after '^'

4) Extract numbers from string:

int a, b, c;

sscanf(“2006:03:18”, “%d:%d:%d”, &a, &b, &c); //a = 2006 b = 3 c = 18

5、
    char *str = "type  subtitle:789";
    int number;
    sscanf(str, "subtitle: %lf", &number);
    printf("number:%f\n",number);

Guess you like

Origin blog.csdn.net/poject/article/details/128576209