Hello!

I am writing a program that works with the txt file, it has to delete text from a specific template from there, how to work with files from and to, I know, and how to do it, set such a template so that what is necessary (text) is deleted, replaced?

Thank.

PS If I did not clearly put it, then tell me.

  • Anything like Regular Expressions ? - VioLet
  • one
    It all depends on what you need to do with the text, what kind of processing is required. Maybe it's pretty simple and you can do without regular expressions. - skegg pm

1 answer 1

Regular expressions are well suited for such tasks. They are not included in the standard C, so you need to connect external libraries. POSIX regexp based example:

#include <sys/types.h> #include <regex.h> #include <stdio.h> int main(int argc, char *argv[]) { regex_t regex; int result; char buffer[256]; /* Компилировать, ... - искомый шаблон */ result = regcomp(&regex, "...", 0); if (!result) { /* Выполнить, ... тестовая строка */ result = regexec(&regex, "...", 0, NULL, 0); if (!result) { /* Найдено совпадение: что-то сделать */ } /* Очистить */ regfree(&regex); } return 0; } 
  • Tell me, please, the literature in which you can deeply study the library regex.h (I understood correctly, does it create a regular expression?)? - VladislavMSK
  • Help is on the GNU website: Regular Expression Matching in English, it is quite possible somewhere and in Russian can be found. - stanislav
  • A brief but informative overview of the regexp for C can be found in Johnson Troan’s Application Development on Linux. - skegg
  • It is often easier to use fnmatch (), it uses a set of metacharacters and regular expression matching rules are the same as sh. - avp