How to get substrate this way in c?

/^[^\s]+\s([^\s]+)\s/

In PHP, I can use regex to get substr $1,

How do I do this in C?

It is better if you can do it without regular expression.

UPDATE

Simply put, how do I get werwerurfrom swerwer werwerur y(second)?

+5
source share
5 answers

I recommend using strchr () - finding characters in strings very quickly

#include <string.h>
..
char str [] = "swerwer werwerur y";
char * p1 = NULL, * p2 = NULL;

p1 = strchr (str, '');
p1 ++;
p2 = strchr (p1, '');
if (p2) * p2 = 0;

printf ("found:% s \ n", p1);

, strtok_r() strpbrk(), :

    char str[] = "swerwer ., werwerur + y";
    const char *dlms = " .,+";
    char *p1 = NULL,*p2 = NULL;

    p1 = strpbrk(str,dlms);
    while(strchr(dlms,*p1)) p1++;
    p2 = strpbrk(p1,dlms);
    if(p2) *p2 = 0;

    printf("found: %s\n", p1);

( : strpbrk NULL)

+2

strtok(), .

Strtok man page:

#include <string.h>
...
char *token;
char *line = "LINE TO BE SEPARATED";
char *search = " ";


/* Token will point to "LINE". */
token = strtok(line, search);


/* Token will point to "TO". */
token = strtok(NULL, search);
+2

, , sscanf :

char result[256];  
sscanf("swerwer werwerur y", "%*s %255s", result);
0

char -string, :

char* strText = "word1 word2 word3";
int iTextLen = strlen(strText);
for(int i = 0; i < iTextLen; i++)
{
    if(strText[i] == ' ') //is it a space-character
    {
        //save position or whatever
    }
}

, .

0

If you are using a posix / unix system such as Linux, you can directly use the regex API:

$> man 3 regex

Or google for regcomp()or regexec().

0
source

All Articles