title: How to find a line in a file using regex in C?
body: <p>how can i use Regex Expressions in C programming? 
  for example if i want to find a line in a file </p>
  
  <pre><code>DAEMONS=(sysklogd network sshd !netfs !crond)
  </code></pre>
  
  <p>then print each daemon in separate line like this </p>
  
  <pre><code>sysklogd 
  network 
  sshd 
  !netfs 
  !crond
  </code></pre>
  
  <p>here what i did so far </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;regex.h&gt;
  #define tofind    "[a-z A-Z] $"
  int main(){
   FILE *fp;
   char line[1024];
   int retval = 0;
   char address[256];
   regex_t re;
  
   if(regcomp(&amp;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(&amp;re, address, 0, NULL, 0)) == 0)
        printf("%s\n", address);
   } 
  }
  </code></pre>
  
  <p>Any help would be much appreciated.</p>