Saya juga pernah mengalami "penderitaan" seperti
anda B).
Paling enak, kalau butuh regex, ya pakai PERL.
Namun, terkadang tuntutan hidup memaksa kita
memakai C. Oh nasib.
Saya ikutan milis linuxcprogramming dan pernah
dibantu dengan file program berikut.
Silakan anda, baca, pelototi, pelajari dan
modifikasi sedikit demi sedikit,pasti terbantu.
Jika anda masih bingung, silakan tanya, nanti
mari kita bingung bersama-sama B).
Hizazul Emkom
C loves me. B)
PS: mungkin ada juga yang mengalami cobaan hidup,
menggunakan C untuk akses ke MySQL dan BerkeleyDB
dan untuk regular expresion? mari kita berbagi
pengetahuan.
------------------ start of program -------------
/*
* histring.c
*
* an example of a program that uses regular expresstions.
*
* this started out as example code but has turned out to be quite useful.
*
* Angus Mackay
*/
/* to do: add configure support :-) */
#ifdef __linux__
# define HAVE_GETOPT_H 1
# define HAVE_GETOPT_LONG 1
#endif
#ifdef HAVE_GETOPT_H
# include <getopt.h>
#endif
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
#ifndef VERSION
# define VERSION "?.?.?"
#endif
#define START_OF_MATCH_FMT "\033[01;%dm"
#define END_OF_MATCH_FMT "\033[0m"
// this is black
#define COLOR_BASE 30
#define DEFAULT_COLOR_CODE 31
static char *g_program_name;
static int g_regcomp_options = 0;
static int g_color_code = DEFAULT_COLOR_CODE;
static char g_start_of_match[16];
static char g_end_of_match[16];
static int (*do_work)(char *, FILE *, FILE *) = NULL;
/*
* this takes a regular expression string and does vt100 based
* hilighting on a file stream
*/
int histring(char *regs, FILE *in, FILE *out)
{
regex_t regex;
regmatch_t regmatch;
int res;
char buf[BUFSIZ+1];
char *p;
int regflags;
// compile our regular expression
if((res=regcomp(®ex, regs, REG_NEWLINE | g_regcomp_options)) != 0)
{
regerror(res, ®ex, buf, BUFSIZ);
fputs(buf, stderr);
fputs("\n", stderr);
return(-1);
}
regflags = 0;
while(fgets(buf, BUFSIZ, in) != NULL)
{
// match start of line to begin with
regflags = 0;
p = buf;
for(;;)
{
// execute our regular expression, this will leave start and end
// markerns in regmatch if there is a match.
if(p && regexec(®ex, p, 1, ®match, regflags) == 0)
{
// handle NULL patterns that we matched
if(regmatch.rm_eo - regmatch.rm_so == 0)
{
if(*p == '\0')
{
break;
}
fputc(*p, out);
p++;
}
else
{
// print up to the match
fwrite(p, 1, regmatch.rm_so, out);
fputs(g_start_of_match, out);
// print the matched portion
fwrite(p+regmatch.rm_so, 1, regmatch.rm_eo - regmatch.rm_so, out);
fputs(g_end_of_match, out);
// increment our pointer
p += regmatch.rm_eo;
}
}
else
{
// no match in this string so just print it
fputs(p, out);
break;
}
// don't match any more start of lines
regflags |= REG_NOTBOL;
}
}
// free the patter buffer
regfree(®ex);
return 0;
}
int cat(char *regs, FILE *in, FILE *out)
{
char buf[BUFSIZ];
int nread;
while((nread=fread(buf, 1, BUFSIZ, in)) > 0)
{
if(fwrite(buf, 1, nread, out) != nread)
{
perror("fwrite");
return(-1);
}
}
return 0;
}
void print_useage( void )
{
fprintf(stdout, "useage: ");
fprintf(stdout, "%s [options] <pattern> [file]...\n\n", g_program_name);
fprintf(stdout, " Options are:\n");
fprintf(stdout, " -c, --color <name|number>\tcolor to highlight in\n");
fprintf(stdout, " -E, --extended \t\tuse extended regular expressions\n");
fprintf(stdout, " -f, --force \t\t\tforce hilighting even if stdout is not a
tty\n");
fprintf(stdout, " -i, --ignore-case \t\tguess what this one does\n");
fprintf(stdout, " --help\t\t\tdisplay this help and exit\n");
fprintf(stdout, " --version\t\t\toutput version information and exit\n");
fprintf(stdout, " --credits\t\t\tprint the credits and exit\n");
fprintf(stdout, "\n");
}
void print_version( void )
{
fprintf(stdout, "%s: - %s - $Id: $\n", g_program_name, VERSION);
}
void print_credits( void )
{
fprintf(stdout, "AUTHORS / CONTRIBUTORS\n"
" Angus Mackay <[EMAIL PROTECTED]>\n"
"\n");
}
#ifdef HAVE_GETOPT_LONG
# define xgetopt( x1, x2, x3, x4, x5 ) getopt_long( x1, x2, x3, x4, x5 )
#else
# define xgetopt( x1, x2, x3, x4, x5 ) getopt( x1, x2, x3 )
#endif
void parse_args( int argc, char **argv )
{
#ifdef HAVE_GETOPT_LONG
struct option long_options[] = {
{"color", required_argument, 0, 'c'},
{"extended", no_argument, 0, 'E'},
{"force", no_argument, 0, 'f'},
{"ignore-case", no_argument, 0, 'i'},
{"help", no_argument, 0, 'H'},
{"version", no_argument, 0, 'V'},
{"credits", no_argument, 0, 'C'},
{0,0,0,0}
};
#else
# define long_options NULL
#endif
int opt;
static struct color_t
{
char *name;
int value;
} colors[] = {
{ "black", 0 },
{ "red", 1 },
{ "green", 2 },
{ "yellow", 3 },
{ "blue", 4 },
{ "magenta", 5 },
{ "cyan", 6 },
{ "white", 7 },
{ 0, 0 }
}, *color;
int found;
while((opt = xgetopt(argc, argv, "c:EfiHVC", long_options, NULL)) != -1)
{
switch (opt)
{
case 'c':
color = colors;
found = 0;
while(color->name != 0)
{
if(strcmp(color->name, optarg) == 0)
{
g_color_code = COLOR_BASE + color->value;
found = 1;
break;
}
color++;
}
if(!found)
{
if(isdigit(*optarg))
{
g_color_code = COLOR_BASE + atoi(optarg);
}
else
{
fprintf(stderr, "unknown color: %s\n", optarg);
}
}
break;
case 'E':
g_regcomp_options |= REG_EXTENDED;
break;
case 'f':
// force hilighting
do_work = histring;
break;
case 'i':
g_regcomp_options |= REG_ICASE;
break;
case 'H':
print_useage();
exit(0);
break;
case 'V':
print_version();
exit(0);
break;
case 'C':
print_credits();
exit(0);
break;
default:
#ifdef HAVE_GETOPT_LONG
fprintf(stderr, "Try `%s --help' for more information\n", argv[0]);
#else
fprintf(stderr, "Try `%s -H' for more information\n", argv[0]);
fprintf(stderr, "warning: this program was compilied without getopt_long\n");
fprintf(stderr, " as such all long options will not work!\n");
#endif
exit(1);
break;
}
}
if((argc-optind) < 1)
{
#ifdef HAVE_GETOPT_LONG
fprintf(stderr, "Try `%s --help' for more information\n", argv[0]);
#else
fprintf(stderr, "Try `%s -H' for more information\n", argv[0]);
fprintf(stderr, "warning: this program was compilied without getopt_long\n");
fprintf(stderr, " as such all long options will not work!\n");
#endif
exit(1);
}
}
int main(int argc, char **argv)
{
int i;
FILE *fp;
g_program_name = argv[0];
// use hilighting by default
do_work = histring;
if(!isatty(1))
{
// just copy the file to stdout
do_work = cat;
}
parse_args(argc, argv);
sprintf(g_start_of_match, START_OF_MATCH_FMT, g_color_code);
sprintf(g_end_of_match, END_OF_MATCH_FMT);
// set all buffering to line buffering so that you can chain
// multiple histrings for different colors
setvbuf(stdin, NULL, _IOLBF, BUFSIZ);
setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
if(argc-optind < 2)
{
do_work(argv[optind], stdin, stdout);
}
else
{
for(i=optind+1; i<argc; i++)
{
if((fp=fopen(argv[i], "r")) != NULL)
{
setvbuf(fp, NULL, _IOLBF, BUFSIZ);
do_work(argv[optind], fp, stdout);
}
else
{
perror(argv[i]);
}
fclose(fp);
}
}
return 0;
}
------------------- end of program --------------
-----Original Message-----
From: "commedo" <[EMAIL PROTECTED]>
Date: Fri, 23 Jun 2000 15:02:09 +0700
To: "Linux Programming" <[EMAIL PROTECTED]>
Subject: [programming] Regex pake C
> Ada yang punya pengalaman dengan yang namanya regex pake C nggak ????
> Kalo pernah, tolongin doonggg. Atawa referensinya.
>
> Makasih banyak.
>
> Slamet
> e-mail : [EMAIL PROTECTED]
> home : <A HREF="http://www.commedo.com" TARGET="_new"><FONT
>COLOR="BLUE">http://www.commedo.com</FONT></A>
>
> --------------------------------------------------------------------------------
> Utk berhenti langganan, kirim email ke [EMAIL PROTECTED]
> Informasi arsip di <A HREF="http://www.linux.or.id/milis.php3" TARGET="_new"><FONT
>COLOR="BLUE">http://www.linux.or.id/milis.php3</FONT></A>
> Pengelola dapat dihubungi lewat [EMAIL PROTECTED]
>
>
--
_____________________________________________________________
satusports.com, situs olahraga nomor satu dan terlengkap Indonesia.
Untuk informasi terakhir sepakbola, bolabasket, tinju, golf, dan olahraga lainnya,
kliklah http://www.satusports.com
Powered by OutBlaze
--------------------------------------------------------------------------------
Utk berhenti langganan, kirim email ke [EMAIL PROTECTED]
Informasi arsip di http://www.linux.or.id/milis.php3
Pengelola dapat dihubungi lewat [EMAIL PROTECTED]