how can i use Regex Expressions in C programming?
for example if i want to find a line in a file
DAEMONS=(sysklogd network sshd !netfs !crond)
then print each daemon in separate line like this
sysklogd
network
sshd
!netfs
!crond
here what i did so far
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>
#define tofind "[a-z A-Z] $"
int main(){
FILE *fp;
char line[1024];
int retval = 0;
char address[256];
regex_t re;
if(regcomp(&re, tofind, REG_EXTENDED) != 0)
return;
fp = fopen("/etc/rc.conf","r");//this file has this line "DAEMONS=(sysklogd network sshd !netfs !crond)"
while((fgets(line, 1024, fp)) != NULL) {
if((retval = regexec(&re, address, 0, NULL, 0)) == 0)
printf("%s\n", address);
}
}
Any help would be much appreciated.
You read the line into
line, so you should passlinetoregexec(). You also need to think about whether the newline at the end of the line affects the patterns. (It was correct to usefgets(), but remember it keeps the newline at the end.)You should also do
return -1;(or any other value that is not 0 modulo 256) rather than a plainreturnwith no value. Also, you should check that the file was opened; I had to use an alternative name because there is no such file as /etc/rc.conf on my machine – MacOS X.This works for me:
If you need help writing regular expressions instead of help writing C code that uses them, then we need to design the regex to match the line you show.
This will match the line as long as it is written as shown. If you can have spaces between the ‘
S‘ and the ‘=‘ or between the ‘=‘ and the ‘(‘, then you need appropriate modifications. I’ve allowed for trailing blanks – people are often sloppy; but if they use trailing tabs, then the line won’t be selected.Once you’ve found the line, you have to split it into pieces. You might elect to use the ‘capturing’ brackets facility, or simply use
strchr()to find the open bracket, and then a suitable technique for separating the daemon names – I’d avoidstrtok()and probably usestrspn()orstrcspn()to find the words.A good deal of debugging code in there – but it won’t take you long to produce the answer you request. I get:
Beware: when you want a backslash in a regex, you have to write two backslashes in the C source code.