regex
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > regex




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    regex  
Greg Martin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:35 PM

RFC 2396 on URI's gives the regular expression stored in the string regex
in the code below. The expression compiles successfully but produces no
matches. I'm afraid I know little about regular expressions. I'd appreciate
any help - an explanation for why there is no match here, the corrected
expression or resources for developing a good understanding of regex and
regular expressions.
Thanks.
//
// gcc -Wall regextest.cpp -o regextest -lstdc++ -lm
//
#include <sys/types.h>
#include <regex.h>

#include <iostream>
#include <string>

int main()
{
std::string regex =
"^(([^:/?#]+)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?";
std::string uri = "http://www.ics.uci.edu/pub/ietf/uri/#Related";

regex_t preg;

int rv = regcomp(&preg, regex.c_str(), 0);

if(rv != 0)
{
size_t sz = 128;
resize1:
char *errb = new char[sz];
size_t bs = regerror(rv, &preg, errb, sz);

if(bs < sz)
std::cout << errb << "\n";
else
{
sz = bs;
delete errb;
goto resize1;
}
delete errb;
exit(0);
}

std::cout << "Compiled successfully\n";

regmatch_t pmatch[9];
rv = regexec(&preg,  uri.c_str(), 9, pmatch, 0);

if(rv != 0)
{
size_t sz = 128;
resize2:
char *errb = new char[sz];
size_t bs = regerror(rv, &preg, errb, 256);

if(bs < sz)
std::cout << errb << "\n";
else
{
sz = bs;
delete errb;
goto resize2;
}

delete errb;
regfree(&preg);
exit(0);
}

std::cout << "Executed successfully\n";

for(int i = 0; i < 9; ++i)
{
std::cout << i << " : ";
std::cout << pmatch[i].rm_so << " x ";
std::cout << pmatch[i].rm_eo << "\n";

}

regfree(&preg);

return 0;
}

--
Greg Martin
gregmar found at telusplanet on the net





[ Post a follow-up to this message ]



    Re: regex  
joe@invalid.address


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:35 PM

Greg Martin <gregmar@telusplanet.net> writes:
quote:
> RFC 2396 on URI's gives the regular expression stored in the string regex > in the code below. The expression compiles successfully but produces no > matches. I'm afraid I know little about regular expressions. I'd appreciat e > any help - an explanation for why there is no match here, the corrected > expression or resources for developing a good understanding of regex and > regular expressions. > Thanks. > // > // gcc -Wall regextest.cpp -o regextest -lstdc++ -lm > // > #include <sys/types.h> > #include <regex.h> > > #include <iostream> > #include <string> > > int main() > { > std::string regex = > "^(([^:/?#]+)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?";
This should work with perl, but in C/C++ you need to quote the backslash. Try it with "^(([^:/?#]+)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?"; Joe




[ Post a follow-up to this message ]



    Re: regex  
Mike Chirico


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:35 PM


"Greg Martin" <gregmar@telusplanet.net> wrote in message
news:AOyMb.12158$De.11896@edtnps84...
quote:
> RFC 2396 on URI's gives the regular expression stored in the string regex > in the code below. The expression compiles successfully but produces no > matches. I'm afraid I know little about regular expressions. I'd
appreciate
quote:
> any help - an explanation for why there is no match here, the corrected > expression or resources for developing a good understanding of regex and > regular expressions. > Thanks. > // > // gcc -Wall regextest.cpp -o regextest -lstdc++ -lm > // > #include <sys/types.h> > #include <regex.h> > > #include <iostream> > #include <string> > > int main() > { > std::string regex = > "^(([^:/?#]+)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?"; > std::string uri = "http://www.ics.uci.edu/pub/ietf/uri/#Related"; > > regex_t preg;
[ snip] Here's a C sample, that may help: #include <stdio.h> #include <sys/types.h> #include <string.h> #include <regex.h> / **************************************** *************** gcc -o regexp regexp.c ./regexp '([a|c|e|g]{2}|[h-z])([0-9]|-)(a|b)' cc8b match 4 0 $0 = cc8b, preg.re_nsub = 3 2 0 $1 = cc, preg.re_nsub = 3 3 2 $2 = 8, preg.re_nsub = 3 4 3 $3 = b, preg.re_nsub = 3 A html copy of this program can be found at http://souptonuts.sourceforge.net/code/C_regexp.c.html **************************************** ***************/ #define NUM_MATCHES 4//max sub-matches int main (int argc, char *argv[]) { regex_t preg; regmatch_t pmatch[NUM_MATCHES]; size_t rm, i; char pom[1024]; if (argc < 2) { printf ("Usage ./regexp [a-z]{2}[0-9]{2} a2b3ed23\n"); return 0; } rm = regcomp (&preg, argv[1], REG_EXTENDED); (rm = regexec (&preg, argv[2], NUM_MATCHES, pmatch, 0)) ? printf ("No match\n") : printf ("match\n"); for (i = 0; !rm && i <= preg.re_nsub; i++) { strncpy (pom, argv[2] + pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so); printf ("%d %d\n", pmatch[i].rm_eo, pmatch[i].rm_so); pom[pmatch[i].rm_eo - pmatch[i].rm_so] = '\0'; printf ("$%d = %s, preg.re_nsub = %d\n", i, pom, preg.re_nsub); } regfree (&preg); return 0; } Regards, Mike Chirico




[ Post a follow-up to this message ]



    Re: regex  
Greg Martin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:35 PM

joe@invalid.address wrote:
quote:
> Greg Martin <gregmar@telusplanet.net> writes: > > > This should work with perl, but in C/C++ you need to quote the > backslash. Try it with > > "^(([^:/?#]+)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?";
I tried that but it still doesn't produce a match for me. I tried with REG_EXTENDED as well - still no match. -- Greg Martin gregmar found at telusplanet on the net




[ Post a follow-up to this message ]



    Re: regex  
joe@invalid.address


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:35 PM

Greg Martin <gregmar@telusplanet.net> writes:
quote:
> joe@invalid.address wrote: > > > I tried that but it still doesn't produce a match for me. I tried with > REG_EXTENDED as well - still no match.
Odd, I tried it with the Sun Workshop 6.x CC and it worked fine for me. It doesn't work for me with gcc or g++ (why are you compiling a C++ program with gcc rather than g++?). I've got gcc version: $ g++ --version g++ (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5) Copyright (C) 2002 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. It may be a glibc problem. The RE looks ok to me. Joe




[ Post a follow-up to this message ]



    Re: regex  
John W. Krahn


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:35 PM

Greg Martin wrote:
quote:
> > RFC 2396 on URI's gives the regular expression stored in the string regex > in the code below. The expression compiles successfully but produces no > matches. I'm afraid I know little about regular expressions. I'd appreciat e > any help - an explanation for why there is no match here, the corrected > expression
Well it works in PERL but I don't know enough about C++ to see what is wrong . $ PERL -le' my $preg = qr"^(([^:/?#]+)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?"; my $uri = "http://www.ics.uci.edu/pub/ietf/uri/#Related"; my @matches = $uri =~ $preg; print for @matches; ' http: http //www.ics.uci.edu www.ics.uci.edu /pub/ietf/uri/ #Related Related
quote:
> or resources for developing a good understanding of regex and > regular expressions.
For a good understanding of regular expressions get the book "Mastering Regu lar Expressions". http://www.oreilly.com/catalog/regex2/index.html John -- use Perl; program fulfillment




[ Post a follow-up to this message ]



    Re: regex  
Valentin Nechayev


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:35 PM

>>> Greg Martin wrote:
[QUOTE] 

Yes, this is important: as it is string source used by C compiler,
\\ is translated to single backslash in resulting code.

GM> I tried that but it still doesn't produce a match for me. I tried with
GM> REG_EXTENDED as well - still no match.

You should try REG_EXTENDED from the very beginning. This regexp is
extended POSIX regexp, not basic. Also, PERL regexps are extensions
of extended POSIX regexps, not basic POSIX regexps.

I tried to run your program on FreeBSD 4.9 (regexp routines from libc).
It runs successfully with change of single backslash to double in C string
source. I can't imagine what another problem you met. Show regerror() output
.


-netch-





[ Post a follow-up to this message ]



    Re: regex  
Greg Martin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:35 PM

joe@invalid.address wrote:
quote:
> Greg Martin <gregmar@telusplanet.net> writes: > > > Odd, I tried it with the Sun Workshop 6.x CC and it worked fine for > me. It doesn't work for me with gcc or g++ (why are you compiling a > C++ program with gcc rather than g++?). >
Just habit (gcc runs the C++ compiler any way). der if ther -- Greg Martin gregmar found at telusplanet on the net




[ Post a follow-up to this message ]



    Re: regex  
Greg Martin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:35 PM

Valentin Nechayev wrote:
quote:
> > > Yes, this is important: as it is string source used by C compiler, > \\ is translated to single backslash in resulting code. > > GM> I tried that but it still doesn't produce a match for me. I tried with > GM> REG_EXTENDED as well - still no match. > > You should try REG_EXTENDED from the very beginning. This regexp is > extended POSIX regexp, not basic. Also, PERL regexps are extensions > of extended POSIX regexps, not basic POSIX regexps. > > I tried to run your program on FreeBSD 4.9 (regexp routines from libc). > It runs successfully with change of single backslash to double in C string > source. I can't imagine what another problem you met. Show regerror() > output. > >
"No match" is all it returns. I used Debain. I'll try it on some other platforms. -- Greg Martin gregmar found at telusplanet on the net




[ Post a follow-up to this message ]



    Re: regex  
Espen Myrland


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:35 PM

Greg Martin <gregmar@telusplanet.net> writes:
quote:
> > "No match" is all it returns. I used Debain. I'll try it on some other > platforms. >
With both the above additions, it works here on Debian, gcc 2.95.4, libc-2.3.2 -- /espen




[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 01:23 PM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register