mnajem 0 Report post Posted May 17, 2007 Salam,Saya nak enable PCRE usage untuk string manipulation/search guna C. Tengok kat internet banyak scripting yg guna,tapi tidak untuk C.Kebiasaannya string manipulation guna stcmp dan strtok kan.Contoh di sini:http://www.nathanr.net/programming/hints/pcre.shtmlBeliau enablekan pcre.h pada header (saya boleh run kod beliau,dengan syarat ada libpcre<version>-dev).Untuk function:pcre_compilepcre_exec Bagaimana sintaks supaya saya boleh mintak pengguna masukkan pattern & kemudian pcre_compile & pcre_exec akan interpret? Quote Share this post Link to post Share on other sites
encik pot pet 0 Report post Posted May 17, 2007 berdasarkan contoh yg diberi,re = pcre_compile("(\\S+)\\s*:\\s*(\\S+)", 0, &error, &erroffset, NULL); matches = pcre_exec(re, NULL , subject, strlen(subject), 0, 0, ovector, 30); pattern yg dikehendaki adalah berbentuk c-string - ("(\\S+)\\s*:\\s*(\\S+)", maknanya, kita boleh ganti dgn string kita sendiri melalui pointer. printf("Sila masukkan pattern: "); fgets(szPattern,75,stdin); re = pcre_compile(szPattern, 0, &error, &erroffset, NULL); Quote Share this post Link to post Share on other sites
mnajem 0 Report post Posted May 17, 2007 (edited) possible tak kalau kita buat satu file .txt yang ada rules & minta dia interpret.say dalam satu file;ayam.txt^[a-z][A-Z][0-9]akak^[x-z]orroguna fopen ke eh? -aku dah lama tak buat programming.-zeph,pada pendapat ko, exact string matching macam Boyer Moore,KMP,Zhu Takoaka dengan Regular Expression mana yang pantas cari string/manipulate.atau dalam PCRE engine ni pun guna string search algorithm jugak?sebab aku perasan Snort & GNU Grep guna Boyer Moore. Tapi dia accept Regexp sebagai pattern .aku sebenarnye cuba nak implement so called extended version KMP dengan accept Regular Expression,tapi tak tau buat mcm mana,sebab say pattern macam ni:ATAFHAH AHFTHA HAHTHAHHaku nak fetch guna "exact string matching" tak boleh guna wildcard i.e:AT*sebaliknya string matching algo ni accept raw text i.e ATAF (cuma dia akan skip kalo ada mismatch). Edited May 17, 2007 by mnajem Quote Share this post Link to post Share on other sites
encik pot pet 0 Report post Posted May 17, 2007 possible tak kalau kita buat satu file .txt yang ada rules & minta dia interpret.say dalam satu file;ayam.txt^[a-z][A-Z][0-9]akak^[x-z]orroguna fopen ke eh? -aku dah lama tak buat programming.memang kena pakai fopen kalau nak simpan regexp string dalam file. kena ada sedikit string line dan memory allocation.-zeph,pada pendapat ko, exact string matching macam Boyer Moore,KMP,Zhu Takoaka dengan Regular Expression mana yang pantas cari string/manipulate.mungkin anda nak pakai http://en.wikipedia.org/wiki/Fuzzy_string_searchingatau dalam PCRE engine ni pun guna string search algorithm jugak?sebab aku perasan Snort & GNU Grep guna Boyer Moore. Tapi dia accept Regexp sebagai pattern .aku sebenarnye cuba nak implement so called extended version KMP dengan accept Regular Expression,tapi tak tau buat mcm mana,sebab say pattern macam ni:ATAFHAH AHFTHA HAHTHAHHaku nak fetch guna "exact string matching" tak boleh guna wildcard i.e:AT*sebaliknya string matching algo ni accept raw text i.eATAF (cuma dia akan skip kalo ada mismatch).aku tak pernah guna algo semacam ini, oleh itu, tak dapat bagi jawapan yg tepat lah kalau masih nak pakai PRCE ni, boleh guna dirty n quick solution (slow bruteforcing). apply regexp bagi setiap string yg dibaca dari sumber yg dikehendaki.contoh secara pseudo:1. regexp ('ATAFHAH', AT*) if true, found2. regexp ('AHFTHA', AT*) if true, found.... Quote Share this post Link to post Share on other sites
mnajem 0 Report post Posted May 17, 2007 #include <pcre.h> #include <string.h> int main(int argc, char **argv) { pcre *re = NULL; pcre_extra *pe = NULL; const char *error = NULL; int erroffset; int ovector[75]; int matches; const char *match_string; const char *subject; //const char *subject = "foo: bar"; int x; int stdin; int szPattern;         printf("Sila masukkan pattern: ");         fgets(szPattern,75,stdin);         re = pcre_compile(szPattern, 0, &error, &erroffset, NULL); //      re = pcre_compile("(\\S+)\\s*:\\s*(\\S+)", 0, &error, &erroffset, NULL);         matches = pcre_exec(re, NULL , subject, strlen(subject), 0, 0, ovector, 100);         printf("subject=%s\", matches=%d\n", subject, matches);         for (x=0; x < matches; x++)         { pcre_get_substring(subject, ovector, matches, x, &match_string);                 printf("match %d: \"%s\")\n", x, match_string); pcre_free_substring(match_string);         }         return 0; } aku dapat error ni: >gcc mycode.c  -o mycode -lpcre mycode.c: In function ‘main’: mycode.c:16: warning: incompatible implicit declaration of built-in function ‘printf’ mycode.c:18: warning: passing argument 1 of ‘pcre_compile’ makes pointer from integer without a cast [33] >./mycode Sila masukkan pattern: subject=�$�,j", matches=-1 Segmentation fault (core dumped)--time compile warning saja.. tapi segementation fault bila run. Quote Share this post Link to post Share on other sites
encik pot pet 0 Report post Posted May 17, 2007 problemnya, int szPattern; ->> char szPattern[80]={0};kalau ada problem lain2, aku tak dapat tolong sebabzeph@darkthrone:~/pcre$ ./test ./test: error while loading shared libraries: libpcre.so.0: cannot open shared object file: No such file or directory zeph@darkthrone:~/pcre$ whereis libpcre.so libpcre: /usr/local/lib/libpcre.a /usr/local/lib/libpcre.so /usr/local/lib/libpcre.la Quote Share this post Link to post Share on other sites
mnajem 0 Report post Posted May 17, 2007 (edited) whereis libpcre libpcre: /usr/lib/libpcre.a /usr/lib/libpcre.so aku punye libpcre aku dah ubah code tu. ada warning je bila compile & keluar binary. masalahnya ialah program tu tergantung takde output kalo nak kasi output debug guna ltrace kan? ltrace ./mycode __libc_start_main(0x8048564, 1, 0xbf967964, 0x8048730, 0x8048720 <unfinished ...> memset(0xbf96786c, '00', 80)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â = 0xbf96786c printf("Sila masukkan pattern: ")Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â = 23 Edited May 17, 2007 by mnajem Quote Share this post Link to post Share on other sites
encik pot pet 0 Report post Posted May 17, 2007 apabila kita nak menggunakan library yg ditulis org lain (bukan standard c/c++ library) kita kena baca la manual library tersebut. disini reference kepada pcre_exec().#include <pcre.h> int pcre_exec(const pcre *code, const pcre_extra *extra, const char *subject, int length, int startoffset, int options, int *ovector, int ovecsize); DESCRIPTION This function matches a compiled regular expression against a given subject string, using a matching algorithm that is similar to Perl's. It returns offsets to captured substrings. Its arguments are: code Points to the compiled pattern extra Points to an associated pcre_extra structure, or is NULL subject Points to the subject string length Length of the subject string, in bytes startoffset Offset in bytes in the subject at which to start matching options Option bits ovector Points to a vector of ints for result offsets ovecsize Number of elements in the vector (a multiple of 3)awak pulak buatconst char *subject; //const char *subject = "foo: bar";maksudnya subject awak kosong, mmg takde result lah Quote Share this post Link to post Share on other sites
mnajem 0 Report post Posted May 17, 2007 saya agak lambat pikap.apa nilai yang patut saya isi. Quote Share this post Link to post Share on other sites
encik pot pet 0 Report post Posted May 17, 2007 cuba code ni#include <pcre/pcre.h> #include <string.h> int main(int argc, char **argv) { Â Â Â Â pcre *re = NULL; Â Â Â Â pcre_extra *pe = NULL; Â Â Â Â const char *error = NULL; Â Â Â Â int erroffset; Â Â Â Â int ovector[30]; Â Â Â Â int matches; Â Â Â Â const char *match_string; Â Â Â Â const char *subject = "bog : log"; Â Â Â Â int x; Â Â Â Â Â Â Â Â char szPattern[76]={0}; Â Â Â Â printf("Sila masukkan pattern: "); Â Â Â Â fgets(szPattern,75,stdin); Â Â Â Â re = pcre_compile(szPattern, 0, &error, &erroffset, NULL); Â Â Â Â matches = pcre_exec(re, NULL , subject, strlen(subject), 0, 0, ovector, 30); Â Â Â Â printf("subject=\"%s\", matches=%d\n", subject, matches); Â Â Â Â for (x=0; x < matches; x++) Â Â Â Â { Â Â Â Â Â Â Â Â pcre_get_substring(subject, ovector, matches, x, &match_string); Â Â Â Â Â Â Â Â printf("match %d: \"%s\")\n", x, match_string); Â Â Â Â Â Â Â Â pcre_free_substring(match_string); Â Â Â Â } Â Â Â Â return 0; }sila gunakan input sebagai (\\S+)\\s*:\\s*(\\S+) dan boleh tunjuk kat aku result dia sebab aku tak reti nak guna regexp syntax. Quote Share this post Link to post Share on other sites
mnajem 0 Report post Posted May 18, 2007 (edited) gcc zephpcre.c -o zephpcre -lpcre zephpcre.c: In function ‘main’: zephpcre.c:17: warning: incompatible implicit declaration of built-in function ‘printf’ zephpcre.c:18: error: ‘stdin’ undeclared (first use in this function) zephpcre.c:18: error: (Each undeclared identifier is reported only once zephpcre.c:18: error: for each function it appears in.)header tu mula2 takleh guna dua-duazephpcre.c:1:23: error: pcre/pcre.h: No such file or directoryyang betul pcre.h Edited May 18, 2007 by mnajem Quote Share this post Link to post Share on other sites
mchammer 0 Report post Posted June 22, 2007 (edited) Apa kata tambah stdio.h.#include <stdio.h>Edit: Hehe, aku reply thread lama rupanya, tak perasan Edited June 22, 2007 by mchammer Quote Share this post Link to post Share on other sites