Extract substring in C

I am trying to extract the username from this uri field in ANSI C code on linux using gcc

mail:username@example.com 

so I need to remove the mail: and everything after @. Are there built-in functions in C for extracting substrings

+7
c linux
source share
4 answers
 char *uri_field = "mail:username@example.com"; char username[64]; sscanf(uri_field, "mail:%63[^@]", username); 

If you may have another "junk" at the beginning (not necessarily just mail: , you can do something like this:

 sscanf(uri_field, "%*[^:]:%63[^@]", username); 
+8
source share

You can also use strtok . Look at this example

 /* strtok example */ #include <stdio.h> #include <string.h> int main () { char str[] ="mail:username@example.com"; char * pch; pch = strtok (str," :@"); while (pch != NULL) { printf ("%s\n",pch); pch = strtok (NULL, " :@"); } return 0; } 

Hope this helps.

+3
source share
 void getEmailName(const char *email, char **name /* out */) { if (!name) { return; } const char *emailName = strchr(email, ':'); if (emailName) { ++emailName; } else { emailName = email; } char *emailNameCopy = strdup(emailName); if (!emailNameCopy) { *name = NULL; return; } char *atSign = strchr(emailNameCopy, '@'); if (atSign) { *atSign = '\0'; // To remove the '@' // atSign[1] = '\0'; // To keep the '@' } if (*name) { strcpy(*name, emailNameCopy); } else { *name = emailNameCopy; } } 

This creates a pointer to the character : ( colon ) inside the string. (It does not create a copy of the string.) If found : indicate a character after it. If : does not exist, simply use the beginning of the line (i.e. do not accept the mail: prefix).

Now we want to remove everything from @ forward, so we create a copy of the string ( emailNameCopy ), and then trim @ .

The code then creates a pointer to the @ (atSign) character inside the string. If the @ character exists (i.e., Strchr returns non-NULL), the character in @ set to zero, marking the end of the line. (New copy not made.)

Then we return the string or copy it if a buffer was specified.

0
source share

Another solution that does not rely on any particular feature and is easily capable of detecting errors is as follows. Note that you need to free the line when the extractUsername () function succeeds.

Note that in C, you simply move around in a sequence of characters using pointer arithmetic. There are several standard library functions, but they are much simpler than anything you can extract from a string.

There are other problems to detect errors, for example, the presence of more than one "@", for example. But that should be enough as a starting point.

 // Extract "mail:username@example.com" #include <stdio.h> #include <stdlib.h> #include <string.h> const char * MailPrefix = "mail:"; const char AtSign = '@'; char * extractUserName(const char * eMail) { int length = strlen( eMail ); char * posAtSign = strrchr( eMail, AtSign ); int prefixLength = strlen( MailPrefix ); char * toret = (char *) malloc( length + 1 ); if ( toret != NULL && posAtSign != NULL && strncmp( eMail, MailPrefix, prefixLength ) == 0 ) { memset( toret, 0, length +1 ); strncpy( toret, eMail + prefixLength, posAtSign - prefixLength - eMail ); } else { free( toret ); toret = NULL; } return toret; } int main() { const char * test = "mail:baltasarq@gmail.com"; char * userName = extractUserName( test ); if ( userName != NULL ) { printf( "User name: '%s'\n", userName ); free( userName ); } else { fprintf( stderr, "Error: invalid e.mail address\n" ); return EXIT_FAILURE; } return EXIT_SUCCESS; } 
0
source share

All Articles