Il mio problema è riuscire ad estrapolare da tutto il sorgente html il mio ip.
Basandomi su una determinata pagina e guardando l'html è semplice, ma vorrei una soluzione universale, che vada bene con questo o quell'host.
Grazie
Moderatore: Staff
Codice: Seleziona tutto
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <regex.h>
#include <string.h>
#include <stdlib.h>
int main (int argc, char **argv)
{
char str[] = "xxxHello worldip 12.34.56.78abc hello !!!";
char pat[] = "[0-255]\\.[0-255]\\.[0-255]\\.[0-255]";
char *smatch;
regex_t compile;
regmatch_t first_match;
int retval, set;
if (regcomp (&compile, pat, REG_EXTENDED)) {
fprintf (stderr, "regcomp() failed !\n");
_exit (1);
}
if ((retval = regexec (&compile, str, 1, &first_match, 0)) == REG_NOMATCH) {
fprintf (stdout, "No match found in str !\n");
_exit (0);
}
else if (retval) {
fprintf (stderr, "regexec() failed !\n");
_exit (1);
}
regfree (&compile);
set = first_match.rm_eo - first_match.rm_so; /* end offset - start offset */
smatch = (char*) malloc (set + 1);
strncpy (smatch, str + first_match.rm_so, set);
fprintf (stdout, "Match found: \"%s\"\n", smatch);
return 0;
}

Codice: Seleziona tutto
char pattern[] = "([0-9]{1,3}\\.){,3}([0-9]{1,3})"; 
Codice: Seleziona tutto
char pattern[] = "(2[0-5][0-5]\\.|1[0-9][0-9]\\.|[0-9]{1,2}\\.){3}"
"(2[0-5][0-5]|1[0-9][0-9]|[0-9]{1,2})"; 
Codice: Seleziona tutto
char* pattern = "((2[0-5][0-5]|2[0-4][0-9]|1[0-9][0-9]|"
"[1-9][0-9]|[0-9])\\.){3}(2[0-5][0-5]|2[0-4][0-9]|"
"1[0-9][0-5]|[1-9][0-9]|[0-9])"