tags:

views:

420

answers:

5

I have a string input to my program of the form:

con*.cc

I want this to represent the regular expression, con.*.cc.

How can I do this inside a C program? Is there something like sed that I can use?

+2  A: 

You could use PCRE for matching regular expressions. The actual expression could be stored as a char array (like any other string).

codelogic
+3  A: 

I'm guessing you really just want globbing rather than full on regular expressions. If you're running under Linux, you can use the glob function. I imagine there's a more portable way of doing it, but I don't know of one offhand.

Evan Shaw
+1  A: 

What exactly would you like to do with that regex? Assuming, that you will want to do a search on a set of strings that you already have in your code, you will need to use a regex library. C does not support regular expressions natively. Look up the gcc manual -- here.

dirkgently
A: 

Use sed from within your program:

  1. fork()
  2. set stdin/stdout in the child process
  3. exec(sed s/X/Y/) in the child (or whatever operation you want)
  4. parent pushes data into sed, reads data from sed
  5. parent reaps sed when done
bstpierre
A: 

This page describes the <regex.h> header, which implements POSIX regular expression support for C. To use this, you just do:

#include <sys/types.h>
#include <regex.h>

They're fairly nice and easy to work with, but as @Chickencha said in a different answer, it looks as if you're after just globbing filenames, not doing true regular expression matching. Just thought I'd point out that C does indeed have support for regular expressions, since nobody had.

unwind