Research on pattern matching in Linux

Source: Internet
Author: User

# Include
# Include
# Include
# Include
# Include

// Match the input mode with the string to be matched. If the input mode is successful, 0 is returned;
// Pattern: Mode
// Buf: the string to be matched;
Int Regexp (char * pattern, char * BUF)

{
Regex_t reg;
Int status = 0;
Int cflags = reg_extended | reg_nosub;
Char error [32];

/* Compile a regular expression */
Status = regcomp (, pattern, cflags );
If (status! = 0)
{
Regerror (status, error, sizeof (error ));
Printf ("regcomp error-> % s: Pattern '% s' \ n", pattern, error );
Return-1;
}

// Match
Status = regexec (, Buf, 0, null, 0 );
If (status! = 0)
{
If (status! = Reg_nomatch)
{
Regerror (status, error, sizeof (error ));
Printf ("regexec-> non reg_nomatch-> error-> Status = % d: '\ n", error );
Status =-2;
}
Else if (status = reg_nomatch)
{
Printf ("regexec-> reg_nomatch-> error-> Status = % d: '\ n", error );
Status =-3;
}
}
Else
{
Printf ("regexec-> reg_nomatch-> success-> pattern = % s, Buf = % s \ n", pattern, Buf );
Status = 0;
}

/* Output the processing result */
// Output matching results if necessary;

/* Release the regular expression */
Regfree ();

// Return the processing result;
Return status;

}

/* Main Program */

Int main (INT argc, char ** argv)

{
// Char * pattern = "^ [0-9] * [1-9] [0-9] * $ ";
Char * pattern = "^ [0-9] + $ ";
// Char * pattern = "^ \ D + $ ";
Char * Buf = "00000 ";

Int A = Regexp (pattern, Buf );
Printf ("Main-> Info-> A = % d \ n", );
}
Note: Regular Expression control is not supported.

Www.linuxhao.com is a comprehensive technical website for studying Linux application development. Its categories include Linux Installation Guide, Linux system management, Linux network applications, Linux server technology, Linux programming and development, and Linux download center, provides Linux video tutorials, Linux training courses, Linux technical materials for free download and resource sharing services.

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.