GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
regex_sub(3) rationL User Manual regex_sub(3)

regex_sub – Replaces matches of a regular expression in a string

#include <rationl.h>
char* regex_sub(reg_t re, char* input, char* replace);
    

regex_sub() searches maximal substring that the regular expression given as parameter matches in intput and replaces it with the replace value. Returns the result of the remplacement. The return value will need to be freed by the programmer. If no occurence is found the string returned is simply the same and the input string.

re The regular expressions to match against

input The input string to replace from.

replace The string to replace occurences found with.

regex_sub() The input string with the replaced values. If there is no match in the input string, the return value is the same as the input. The return value will need to be freed by the programmer.

Program that replaces any version number in a file with the latest one

#include <err.h>
#include <rationl.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{

if (argc != 2)
errx(1, "No input file found.");
reg_t re = regex_compile("v(\\d+)\\.(\\d+)\\.(\\d+)");
char *line_buf = NULL;
size_t line_buf_size = 0;
int line_count = 0;
ssize_t line_size;
FILE *fp = fopen(argv[1], "r");
if (!fp)
{
errx(EXIT_FAILURE, "Error opening file '%s'\n", argv[1]);
}
char **groups;
while ((line_size = getline(&line_buf, &line_buf_size, fp) >= 0))
{
char* replaced_value = regex_sub(re, line_buf, "v2.5.0");
printf("old:%snew:%s", line_buf, replaced_value);
free(replaced_value);
}
free(line_buf);
fclose(fp);
regex_free(re);
return 0; }

regex_match(3) regex_search(3)
    
April 5, 2021 rationL 0.1.0

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.