views:

119

answers:

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

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

how should I do it in C?

It's better if can do it without regex,though.

UPDATE

Put simply, how do I get werwerur out of swerwer werwerur y (the second)?

+2  A: 

Use strtok() to find the spaces.

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);
WhirlWind
The string can be arbitary long,so `strtok()` may be inefficient.
httpinterpret
@httpinterpret What do you *really* want? I am not a linear search magician.
WhirlWind
I mean it's unnecessary to tokenize the whole string since I only care about the first two.
httpinterpret
@httpinterpret then stop after the first two... strtok() only finds the next token.
WhirlWind
Oh,it seems I misunderstood `strtok`,let me have a look again.
httpinterpret
Strange,does `strtok` internally caches `line`(or remember history operations) so that `token = strtok(NULL, search); ` can point to `TO`?
httpinterpret
@httpinterpret yes, each time you call strtok, it advances the value it returns to the next token. Thus, it is not thread safe.
WhirlWind
There's a companion function called `strtok_r` that allows the caller to provide the position memory so that it can be thread safe if needed.
Tyler McHenry
it's incorrect: char *line = "LINE TO BE SEPARATED"; <= should be const char*; correct: char line[] = "..."
oraz
`strtok()` is up there as one of the worst functions in the C library. It modifies its input, so you may need to copy it. It keeps state in the function itself, so it's neither thread-safe nor re-entrant (if you try using it on a second string while tokenizing the first one, you've messed up). Try to find a safer function (which might be `strtok_r()`).
David Thornley
A: 

If I understand what you want correctly, sscanf should be able to do the job:

char result[256];  
sscanf("swerwer werwerur y", "%*s %255s", result);
Jerry Coffin
Since the matched length is unknown,so this kind of fixed length `result` may fail under certain cases.
httpinterpret
A: 

You could look for spaces in a char-string by using this:

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
    }
}

If you're looking for the second word, you just have to save the positions of the first space and of the second space and get the character string at the indices between the two.

Simon
+1  A: 

I recommend to use strchr() - it is very fast to find characters in strings

#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);

if you have multiple delimiters, you can use strtok_r() or strpbrk() as in example below:

    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);

(should cleanup code: in case if strpbrk returns NULL)

oraz
A: 

If you are using a posix/unix system like Linux, you could use the regex API directly:

$> man 3 regex

Or google for regcomp() or regexec().

Giórgenes