Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
43
Task/Regular-expressions/C/regular-expressions-1.c
Normal file
43
Task/Regular-expressions/C/regular-expressions-1.c
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <regex.h>
|
||||
#include <string.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
regex_t preg;
|
||||
regmatch_t substmatch[1];
|
||||
const char *tp = "string$";
|
||||
const char *t1 = "this is a matching string";
|
||||
const char *t2 = "this is not a matching string!";
|
||||
const char *ss = "istyfied";
|
||||
|
||||
regcomp(&preg, "string$", REG_EXTENDED);
|
||||
printf("'%s' %smatched with '%s'\n", t1,
|
||||
(regexec(&preg, t1, 0, NULL, 0)==0) ? "" : "did not ", tp);
|
||||
printf("'%s' %smatched with '%s'\n", t2,
|
||||
(regexec(&preg, t2, 0, NULL, 0)==0) ? "" : "did not ", tp);
|
||||
regfree(&preg);
|
||||
/* change "a[a-z]+" into "istifyed"?*/
|
||||
regcomp(&preg, "a[a-z]+", REG_EXTENDED);
|
||||
if ( regexec(&preg, t1, 1, substmatch, 0) == 0 )
|
||||
{
|
||||
//fprintf(stderr, "%d, %d\n", substmatch[0].rm_so, substmatch[0].rm_eo);
|
||||
char *ns = malloc(substmatch[0].rm_so + 1 + strlen(ss) +
|
||||
(strlen(t1) - substmatch[0].rm_eo) + 2);
|
||||
memcpy(ns, t1, substmatch[0].rm_so+1);
|
||||
memcpy(&ns[substmatch[0].rm_so], ss, strlen(ss));
|
||||
memcpy(&ns[substmatch[0].rm_so+strlen(ss)], &t1[substmatch[0].rm_eo],
|
||||
strlen(&t1[substmatch[0].rm_eo]));
|
||||
ns[ substmatch[0].rm_so + strlen(ss) +
|
||||
strlen(&t1[substmatch[0].rm_eo]) ] = 0;
|
||||
printf("mod string: '%s'\n", ns);
|
||||
free(ns);
|
||||
} else {
|
||||
printf("the string '%s' is the same: no matching!\n", t1);
|
||||
}
|
||||
regfree(&preg);
|
||||
|
||||
return 0;
|
||||
}
|
||||
54
Task/Regular-expressions/C/regular-expressions-2.c
Normal file
54
Task/Regular-expressions/C/regular-expressions-2.c
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#include <stdio.h>
|
||||
#include <glib.h>
|
||||
|
||||
void print_regex_match(const GRegex* regex, const char* string) {
|
||||
GMatchInfo* match_info;
|
||||
gboolean match = g_regex_match(regex, string, 0, &match_info);
|
||||
printf(" string = '%s': %s\n", string, match ? "yes" : "no");
|
||||
g_match_info_free(match_info);
|
||||
}
|
||||
|
||||
void regex_match_demo() {
|
||||
const char* pattern = "^[a-z]+$";
|
||||
GError* error = NULL;
|
||||
GRegex* regex = g_regex_new(pattern, 0, 0, &error);
|
||||
if (regex == NULL) {
|
||||
fprintf(stderr, "%s\n", error->message);
|
||||
g_error_free(error);
|
||||
return;
|
||||
}
|
||||
printf("Does the string match the pattern '%s'?\n", pattern);
|
||||
print_regex_match(regex, "test");
|
||||
print_regex_match(regex, "Test");
|
||||
g_regex_unref(regex);
|
||||
}
|
||||
|
||||
void regex_replace_demo() {
|
||||
const char* pattern = "[0-9]";
|
||||
const char* input = "Test2";
|
||||
const char* replace = "X";
|
||||
GError* error = NULL;
|
||||
GRegex* regex = g_regex_new(pattern, 0, 0, &error);
|
||||
if (regex == NULL) {
|
||||
fprintf(stderr, "%s\n", error->message);
|
||||
g_error_free(error);
|
||||
return;
|
||||
}
|
||||
char* result = g_regex_replace_literal(regex, input, -1,
|
||||
0, replace, 0, &error);
|
||||
if (result == NULL) {
|
||||
fprintf(stderr, "%s\n", error->message);
|
||||
g_error_free(error);
|
||||
} else {
|
||||
printf("Replace pattern '%s' in string '%s' by '%s': '%s'\n",
|
||||
pattern, input, replace, result);
|
||||
g_free(result);
|
||||
}
|
||||
g_regex_unref(regex);
|
||||
}
|
||||
|
||||
int main() {
|
||||
regex_match_demo();
|
||||
regex_replace_demo();
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue