Sscanf Regular Expression (2)

Source: Internet
Author: User

Original article link

Header file # include (Stdio. h) <Stdio. h>

Define the function int sscanf (const char * STR, const char * format ,........);

Function Description: sscanf () converts and formats the STR string based on the format string. For more information about format conversion, see scanf (). The converted result is stored in the corresponding parameter.

If the return value is successful, the number of parameters is returned. If the return value fails,-1 is returned. The error cause is stored in errno.

Zhou xing'sCode:

#Include <Stdio.H>

IntMain()
{
Const Char*S= "Iios/12ddwdff @ 122";
CharBuf[20];

Sscanf(S, "% * [^/]/% [^ @]",Buf);
Printf( "% S \ n",Buf);

Return0;
}

Result::12 ddwdff

Similar to scanf, sscanf is used for input, but the latter uses the screen(Stdin)Is the input source, and the former uses a fixed string as the input source.

Function prototype:

Int scanf (const char * Format [, argument]...);
The format can be one or more {% [*] [width] [{H | L | i64 | L}] type | ''| '\ T' | '\ n' | non-% sign },

Note: {A | B | c} indicates A, B, and C. Select [d], which indicates D or D.

Width: width, which can be ignored. Its usage is as follows:

Const char sourcestr [] = "Hello, world ";

Char Buf [10] = {0 };

Sscanf (sourcestr, "% 5 s", Buf); // % 5 s, only 5 Characters

Cout <Buf <Endl;

Result: Hello

{H | L | i64 | L}: parameter size. Generally, h indicates the size of a single byte, I indicates the size of 2 bytes, and l indicates the size of 4 bytes (double exception ), l64 indicates 8-byte size.

Type: this is a lot, that is, % s, % d and so on.

Special:

% * [Width] [{H | L | i64 | L}] type indicates that values that meet this condition are filtered out and no value is written to the target parameter. For example:

Const char sourcestr [] = "Hello, world ";

Char Buf [10] = {0 };

Sscanf (sourcestr, "% * S % s", Buf); // % * s indicates that the first matching % s is filtered out, that is, hello is filtered out.

Cout <Buf <Endl;

Result: World

Collection operations are supported:

% [A-Z] indicates matching any character in A to Z, greedy (as many as possible)

% [AB '] matches a, B, and', greedy

% [^ A] matches any character other than a, greedy

1.Common usage.

The following is a reference clip:
Charstr[512]={0};
Sscanf("123456","% S",Str);
Printf("Str = % s
",Str);

2.Returns the string of the specified length. In the following example, a string with a maximum length of 4 bytes is obtained.

The following is a reference clip:
Sscanf("123456","% 4 s",Str);
Printf("Str = % s
",Str);

3.The string that ends with the specified character. For example, in the following example, the string is obtained when a space is encountered.

The following is a reference clip:
Sscanf("123456 abcdedf","% [^]",Str);
Printf("Str = % s
",Str);

4.Take a string that only contains the specified character set. For example, in the following example, take a string that only contains 1 to 9 letters and lowercase letters.

The following is a reference clip:
Sscanf("123456 abcdedfbcdef","% [1-9a-z]",Str);
Printf("Str = % s
",Str);

5.Obtains the string of the specified character set. For example, in the following example, a string with uppercase letters is used.

The following is a reference clip:
Sscanf("123456 abcdedfbcdef","% [^ A-Z]",Str);
Printf("Str = % s",Str); 

Collect some special usage:

 

%[ ]Usage:%[ ]Indicates that a character set is to be read.,If[The first character after it is "^", which indicates the inverse meaning.

[ ]Can contain 1 or more characters. Empty character set (%[]) Is in violation of regulations, you can

Result in unpredictable results.%[^]It is also in violation of regulations.

%[A-Z]Read in-A string between Z and Z. If it is not earlier, it is stopped, as shown in figure

CharS[]="Hello, my friend"; // Note: comma is not between a-z

Sscanf (S, "% [A-Z]", string); // string = Hello

% [^ A-Z] reads strings not between a-Z. If a character between a-Z is encountered, it is stopped, as shown in figure

Char s [] = "hellokitty"; // Note: the comma is not between a-z

Sscanf(S,"%[^A-Z]", String ) ; // String = Hello

%*[^=]Front band*No. indicates that no variable is saved. Skips a qualified string.

CharS[]="Notepad = 1.0.0.1001" ;

CharSzfilename[32] = "" ;

IntI= Sscanf(S, "% * [^ =]",Szfilename) ;

// Szfilename = NULL because it is not saved

IntI= Sscanf(S, "% * [^ =] = % S",Szfilename) ;

// Szfilename = 1.0.0.1001

%40C reads 40 characters

%[^=]Read the string'=',' ^ 'Can be followed by more characters,For example:

CharS[]="Notepad = 1.0.0.1001" ;

CharSzfilename[32] = "" ;

IntI= Sscanf(S, "% [^ =]",Szfilename) ;

// Szfilename = notepad

If the parameter format is:%[^=:], You can also:1.0.0.1001 read notepad

Refer:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.