LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-14-2008, 06:11 PM   #1
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Rep: Reputation: 30
PCRE problems - how to get the info


Hi...

I am trying to implement a similar solution to this in C:
PHP Code:
<?php
$subject 
"{assign var='crap' value='test'}";
//$pattern = "/^\{([a-zA-Z]+)(\s+(\w+)=['\"](\w+)['\"])* /i";
$pattern "/^\{(\w+)(((\s+)((\w+)=['](\w+)[']))?)+\}$/i";
preg_match($pattern$subject$matchesPREG_OFFSET_CAPTURE0);
print_r($matches);
?>
What I've got so far in C
Code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <pcre.h>

using namespace std;

int main(int argc, char** argv)
{
    pcre *re;
    const char *error;
    int erroffset;
    int ovector[3];
    int rc;
    char *regex = "^\\{(\\w+)(((\\s+)((\\w+)=['\\\"](\\w+)['\\\"]))?)+\\}$";
    char *data = "{assign var=\"test\" value=\"test\"}";
    re = pcre_compile(regex, 0, &error, &erroffset, NULL);
    if (!re)
    {
        cout << erroffset << error;
        return 1;
    }
    pcre_extra *tt;
    rc = pcre_exec(re, tt, data, strlen(data), 0, 0, ovector, 3);
    if (rc < 0)
    {
        switch (rc)
        {
        case PCRE_ERROR_NOMATCH:
            cout << "No match was found";
            break;
        default:
            cout << "Match error: " << rc;
            break;
        }
        return 1;
    }

    char *m = data + ovector[0];   
}
It is good as far as the program gets till the very end. The output of the PHP pplication is:
Quote:
Array
(
[0] => Array
(
[0] => {assign var='crap' value='test'}
[1] => 0
)

[1] => Array
(
[0] => assign
[1] => 1
)

[2] => Array
(
[0] =>
[1] => 31
)

[3] => Array
(
[0] => value='test'
[1] => 18
)

[4] => Array
(
[0] =>
[1] => 18
)

[5] => Array
(
[0] => value='test'
[1] => 19
)

[6] => Array
(
[0] => value
[1] => 19
)

[7] => Array
(
[0] => test
[1] => 26
)

)
I would like to have a similar output in C or at least I would like to know how to access the data. Any thoughts?
 
Old 07-15-2008, 04:33 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,950
Blog Entries: 11

Rep: Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860
What I don't understand is why you're using C functionality
within C++ all the time rather than utilising something like
http://www.daemon.de/PCRE ... a C++ wrapper.


Cheers,
Tink
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Examples of PCRE library in C Centinul Programming 11 09-27-2010 05:13 AM
pcre upgrade on centos 4.6 marcin77 Linux - Server 0 07-14-2008 06:02 PM
How to use PCRE in C ? raiux Programming 1 11-23-2007 06:43 AM
PCRE Regex Osiris990 Programming 10 10-22-2007 02:43 PM
Bluefish -> pcre-devel?? p41elvis Linux - Software 3 04-25-2004 11:10 AM


All times are GMT -5. The time now is 04:42 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration