#include #include #include #include "regex-common.h" int usage(const char *prg) { printf("\nUsage:\n"); printf("%s text 'pattern'\n", prg); printf("%s text '[m]/pattern/[i]'\n", prg); printf("%s text 's/pattern/replacement/[i]'\n\n", prg); printf("# Match, no pattern delimiters\n%s 'Hello, world!' world\nHello, world!\n\n", prg); printf("# Match, case insensitive\n%s 'Hello, world!' 'm/^hello/i'\nHello, world!\n\n", prg); printf("# No match\n%s 'Hello, world!' 'm/^hello/'\n\n\n", prg); printf("# Substitution, match\n%s 'Hello, world!' 's/^Hello/Hi/'\nHi, world!\n\n", prg); printf("# Substitution, no match\n%s 'Hello, world!' 's/^Hola/Hi/'\nHello, world!\n\n", prg); return 1; } int main(int argc, char *argv[]) { char *s_text = NULL; /* argv[1]: original string */ char *s_regex = NULL; /* argv[2]: Perl regex, with possible 'm' or 's' prefix, and modifier flags 'i' */ char *s_result = NULL; /* Returned result from regex_perl() call */ char *p_result = NULL; /* Result to be printed, either s_result, input text, or NULL (no match). */ if (argc < 3) { return usage(argv[0]); } switch (regex_perl(s_text, s_regex, &s_result)) { case RE_NO_MATCH: printf("No match!\n"); /* Print nothing. */ /* p_result = NULL; */ break; case RE_NO_SUBST: printf("No match!\n"); /* In case of no substition, s_result is NULL and does not have to be free()'d */ /* Print the input text. */ p_result = s_text; break; case RE_SUCCESS: printf("Matched and substituted.\n"); p_result = s_result; break; default: printf("Should never come here\n"); break; } printf("Result: '%s'\n", p_result); if (s_result) { free(s_result); } return 0; }