ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Dear all,
I would like to ask you if you know any web site that has a lit of c libraries/functions especially for strings.
I would like to have a good reference when I want to do something with strings and try to answer a question of the form "I need a function that finds in hug string a filename of the format *.html"
Do you know anything like that?
I would like to thank you in advance for your help
... I need a function that finds in hug string a filename of the format *.html ...
I am not sure I understand you, but if you need to make queries whether something is present in an HTML file, you need and HTML parser - not a "C" string library.
Thats a small part of my code.... I want to get out of that string the index.html and or the test.html
Unfortunately it returns no match...
What might I doing wrong here?
Regards
Alex
Code:
/*
** talker.c -- a datagram "client" demo
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>
int main(int argc, char *argv[])
{
int i,reti;
regex_t regex;
reti = regcomp(®ex, ".*\.\(html\|htm\)", 0);
char errormsg[100];
if( reti )
{ fprintf(stderr, "Could not compile regex\n");
exit(1);
}
reti = regexec(®ex, "I am looking inside this string the index.html or any other files in this format like test.html", 0, NULL, 0);
if( !reti ){
printf("Match\n");
}
else if( reti == REG_NOMATCH ){
printf("No match\n");
regerror(reti,®ex,errormsg,sizeof(errormsg));
printf("error text is: %s\n",errormsg);
}
return 0;
}
Thats a small part of my code.... I want to get out of that string the index.html and or the test.html
Unfortunately it returns no match...
What might I doing wrong here?
Regards
Alex
Code:
/*
** talker.c -- a datagram "client" demo
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>
int main(int argc, char *argv[])
{
int i,reti;
regex_t regex;
reti = regcomp(®ex, ".*\.\(html\|htm\)", 0);
char errormsg[100];
if( reti )
{ fprintf(stderr, "Could not compile regex\n");
exit(1);
}
reti = regexec(®ex, "I am looking inside this string the index.html or any other files in this format like test.html", 0, NULL, 0);
if( !reti ){
printf("Match\n");
}
else if( reti == REG_NOMATCH ){
printf("No match\n");
regerror(reti,®ex,errormsg,sizeof(errormsg));
printf("error text is: %s\n",errormsg);
}
return 0;
}
Start from a simpler case, i.e. try to match 'a' in "a".
...
Maybe you need double backslashes.
...
You are doing it apparently wrong - as I wrote earlier in this thread, you need and HTML parser.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <regex.h>
int main(int argc, char *argv[])
{
int i,reti;
size_t nmatch=10;
regex_t regex;
reti = regcomp(®ex, "[^\.]*", REG_EXTENDED);
char errormsg[100];
regmatch_t pmatch[nmatch];
char *string="I am looking inside this string the index.html or any other files in this format like test.htm";
if( reti )
{ fprintf(stderr, "Could not compile regex\n");
exit(1);
}
else{
printf("socket created %d",i);
reti = regexec(®ex, string, nmatch, pmatch, 0);
if( !reti ){
printf("Match\n");
printf("With the whole expression, "
"a matched substring \"%.*s\" is found at position %d to %d.\n",
pmatch[0].rm_eo - pmatch[0].rm_so, &string[pmatch[0].rm_so],
pmatch[0].rm_so, pmatch[0].rm_eo - 1);
printf("With the sub-expression, "
"a matched substring \"%.*s\" is found at position %d to %d.\n",
pmatch[1].rm_eo - pmatch[1].rm_so, &string[pmatch[1].rm_so],
pmatch[1].rm_so, pmatch[1].rm_eo - 1);
}
else if( reti == REG_NOMATCH ){
printf("No match\n");
regerror(reti,®ex,errormsg,sizeof(errormsg));
printf("error text is: %s\n",errormsg);
}
}
regfree(®ex);
return 0;
}
but returning the
Code:
socket created 32767Match
With the whole expression, a matched substring "I am looking inside this string the index" is found at position 0 to 40.
With the sub-expression, a matched substring "" is found at position -1 to -2.
In the give regular expression I try to get only index (from the index.html), well in the ideal case I want to have the "index.html" but I have simplified it a bit.
As you can see it seems to find something but the returned positions seem weird...
You're current regular expression is matching any string which doesn't contain any periods. I think you want a regular expression like: "(\\w*)\\.htm", this matches the whole string "index.htm" and the first subexpression is just "index", the \\'s are to escape the backslashes so C doesn't interpret them, and "\w" is the character class for word characters (i.e. [A-Za-z0-9])
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.