linux c ip过滤 正则表达式 初步代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <regex.h>


int main()
{
	int cflags = REG_EXTENDED;
	regmatch_t pmatch[1];
	const size_t nmatch = 1;
	int status, i;
	regex_t reg;
	char *pattern = "^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$";
	char *buf = "192.168.1.1";
	regcomp(&reg, pattern, cflags);
	status = regexec(&reg, buf, nmatch, pmatch, 0);
	if (status == REG_NOMATCH)
		printf("No Match\n");
	else if(status == 0)
	{
		printf("Match!\n");
	}
	regfree(&reg);

	return 0;
}

你可能感兴趣的:(linux c ip过滤 正则表达式 初步代码)