# 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.