sscanf regex

Discussion in 'C Programming' started by j0mbolar, Jun 16, 2004.

  1. j0mbolar

    j0mbolar Guest

    say you have char buf[] = "string1 string2 string3";

    then you want to use sscanf to match "string3" and store
    it into another array.

    so:

    char buf[] = "string1 string2 string3";
    char array[100];

    sscanf(buf, "what format string should we use?", array);
     
    j0mbolar, Jun 16, 2004
    #1
    1. Advertising

  2. On Wed, 16 Jun 2004, j0mbolar wrote:
    >
    > say you have char buf[] = "string1 string2 string3";
    > then you want to use sscanf to match "string3" and store
    > it into another array.
    >
    > char buf[] = "string1 string2 string3";
    > char array[100];
    >
    > sscanf(buf, "what format string should we use?", array);


    sscanf(buf, "%*s%*s%s", array);

    would be the simplest, I think.

    -Arthur
     
    Arthur J. O'Dwyer, Jun 16, 2004
    #2
    1. Advertising

  3. j0mbolar

    Eric Sosman Guest

    j0mbolar wrote:
    > say you have char buf[] = "string1 string2 string3";
    >
    > then you want to use sscanf to match "string3" and store
    > it into another array.
    >
    > so:
    >
    > char buf[] = "string1 string2 string3";
    > char array[100];
    >
    > sscanf(buf, "what format string should we use?", array);


    If I understand your intent correctly (not certain):

    if (sscanf(buf, "%*s%*s%99s", array) == 1)
    ...

    is what you want. This format

    - Skips any leading white space, gathers the
    longest possible batch of non-white characters,
    and doesn't store them anywhere, then

    - Skips any more white space, gathers another batch
    of non-white characters, and doesn't store them
    anywhere, then

    - Skips any more white space, gathers a third batch
    of non-white characters (but no more than 99 of
    them), stores them in `array', and returns 1 to
    indicate that it has converted and stored one
    field.

    If the scan reaches the end of `buf' before finding all
    three non-white strings, sscanf() returns either zero or
    EOF depending on the exact circumstances.

    --
     
    Eric Sosman, Jun 16, 2004
    #3
  4. (j0mbolar) wrote:
    >say you have char buf[] = "string1 string2 string3";
    >
    >then you want to use sscanf to match "string3" and store
    >it into another array.
    >
    >so:
    >
    >char buf[] = "string1 string2 string3";
    >char array[100];
    >
    >sscanf(buf, "what format string should we use?", array);


    sscanf( buf, "%*[^ ] %*[^ ] %s", array );


    Regards
    --
    Irrwahn Grausewitz ()
    welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
    clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
    clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
     
    Irrwahn Grausewitz, Jun 17, 2004
    #4
  5. j0mbolar

    Dan Pop Guest

    In <> (j0mbolar) writes:

    >say you have char buf[] = "string1 string2 string3";
    >
    >then you want to use sscanf to match "string3" and store
    >it into another array.
    >
    >so:
    >
    >char buf[] = "string1 string2 string3";
    >char array[100];
    >
    >sscanf(buf, "what format string should we use?", array);


    You don't want to use sscanf for this purpose.

    #define TARGET "string3"
    char *p;

    if ((p = strstr(buf, TARGET)) != NULL) {
    memcpy(array, p, strlen(TARGET));
    array[strlen(TARGET)] = 0;
    }

    Of course, for this trivial example, you don't need to extract the
    pattern from the input string, so

    if (strstr(buf, TARGET) != NULL) strcpy(array, TARGET);

    will do.

    The closest you can get in the way of regex support in standard C are
    strstr(), str[c]spn(), strpbrk() and the %[ conversion specifier of
    sscanf.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jun 17, 2004
    #5
  6. j0mbolar

    Dan Pop Guest

    In <> Eric Sosman <> writes:

    >j0mbolar wrote:
    >> say you have char buf[] = "string1 string2 string3";
    >>
    >> then you want to use sscanf to match "string3" and store

    ^^^^^^^^
    >> it into another array.
    >>
    >> so:
    >>
    >> char buf[] = "string1 string2 string3";
    >> char array[100];
    >>
    >> sscanf(buf, "what format string should we use?", array);

    >
    > If I understand your intent correctly (not certain):
    >
    > if (sscanf(buf, "%*s%*s%99s", array) == 1)
    > ...
    >
    >is what you want. This format


    Could you please explain how is this code checking the presence of
    "string3" in buf? I suppose this what the OP meant by "to match".

    Unless I'm missing something obvious, the OP wasn't asking about how to
    extract the third word in buf.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jun 17, 2004
    #6
  7. j0mbolar

    Eric Sosman Guest

    Dan Pop wrote:
    > In <> Eric Sosman <> writes:
    >
    >
    >>j0mbolar wrote:
    >>
    >>>say you have char buf[] = "string1 string2 string3";
    >>>
    >>>then you want to use sscanf to match "string3" and store

    >
    > ^^^^^^^^
    >
    >>>it into another array.
    >>>
    >>>so:
    >>>
    >>>char buf[] = "string1 string2 string3";
    >>>char array[100];
    >>>
    >>>sscanf(buf, "what format string should we use?", array);

    >>
    >> If I understand your intent correctly (not certain):
    >>
    >> if (sscanf(buf, "%*s%*s%99s", array) == 1)
    >> ...
    >>
    >>is what you want. This format

    >
    >
    > Could you please explain how is this code checking the presence of
    > "string3" in buf? I suppose this what the OP meant by "to match".
    >
    > Unless I'm missing something obvious, the OP wasn't asking about how to
    > extract the third word in buf.


    Well, I *said* I wasn't sure I'd understood the O.P.'s
    intent. I thought about offering

    if (strchr(buf, "string3") != NULL)
    strcpy(array, "string3");

    .... but that seemed pointless, so I tried to look for another
    interpretation of an imprecise problem statement. And then I
    told him what my "solution" did, just in case I was solving
    the wrong problem.

    Now I'll get back to devising the circuitry behind the
    DWIM instruction in the next-generation SPARC ;-)

    --
     
    Eric Sosman, Jun 17, 2004
    #7
  8. Eric Sosman <> wrote:
    > Dan Pop wrote:


    [snip]

    >> Could you please explain how is this code checking the presence of
    >> "string3" in buf? I suppose this what the OP meant by "to match".
    >>
    >> Unless I'm missing something obvious, the OP wasn't asking about how to
    >> extract the third word in buf.


    > Well, I *said* I wasn't sure I'd understood the O.P.'s
    > intent. I thought about offering


    > if (strchr(buf, "string3") != NULL)

    ^^^^^^
    > strcpy(array, "string3");


    I think you mean 'strstr'.

    --
    Alex Monjushko ()
     
    Alex Monjushko, Jun 17, 2004
    #8
  9. j0mbolar

    Dan Pop Guest

    In <> Eric Sosman <> writes:

    >Dan Pop wrote:
    >> In <> Eric Sosman <> writes:
    >>
    >>
    >>>j0mbolar wrote:
    >>>
    >>>>say you have char buf[] = "string1 string2 string3";
    >>>>
    >>>>then you want to use sscanf to match "string3" and store

    >>
    >> ^^^^^^^^
    >>
    >>>>it into another array.
    >>>>
    >>>>so:
    >>>>
    >>>>char buf[] = "string1 string2 string3";
    >>>>char array[100];
    >>>>
    >>>>sscanf(buf, "what format string should we use?", array);
    >>>
    >>> If I understand your intent correctly (not certain):
    >>>
    >>> if (sscanf(buf, "%*s%*s%99s", array) == 1)
    >>> ...
    >>>
    >>>is what you want. This format

    >>
    >>
    >> Could you please explain how is this code checking the presence of
    >> "string3" in buf? I suppose this what the OP meant by "to match".
    >>
    >> Unless I'm missing something obvious, the OP wasn't asking about how to
    >> extract the third word in buf.

    >
    > Well, I *said* I wasn't sure I'd understood the O.P.'s
    >intent. I thought about offering
    >
    > if (strchr(buf, "string3") != NULL)
    > strcpy(array, "string3");
    >
    >... but that seemed pointless,


    It was far less pointless than your "solution", because it was
    actually doing the pattern matching required by the OP and *conditionally*
    storing the matched pattern into array.

    >so I tried to look for another
    >interpretation of an imprecise problem statement.


    1. The problem statement was as precise as you can get. It's just that
    sscanf can't be used as part of the solution.

    2. Interpreting a request for pattern matching as a request for finding
    the third word in a string doesn't make any sense to me.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jun 17, 2004
    #9
  10. (Dan Pop) wrote:
    >In <> Eric Sosman <> writes:
    >
    >>j0mbolar wrote:
    >>> say you have char buf[] = "string1 string2 string3";
    >>>
    >>> then you want to use sscanf to match "string3" and store

    > ^^^^^^^^
    >>> it into another array.

    ....
    >> If I understand your intent correctly (not certain):
    >>
    >> if (sscanf(buf, "%*s%*s%99s", array) == 1)
    >> ...

    >
    >Could you please explain how is this code checking the presence of
    >"string3" in buf? I suppose this what the OP meant by "to match".


    Er... well, maybe, but it doesn't make much sense to look for a
    literal occurrence of a certain substring *and then copy that to
    another array*, does it?!?

    >Unless I'm missing something obvious, the OP wasn't asking about how to
    >extract the third word in buf.


    As I understood, the OP was asking exactly about that: how to
    extract the last of three blank separated words from a given
    string, but used the word 'match' in a less strict sense than
    you inferred. I may still be mistaken, though.

    Regards
    --
    Irrwahn Grausewitz ()
    welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
    clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
    clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
     
    Irrwahn Grausewitz, Jun 17, 2004
    #10
  11. j0mbolar

    Dan Pop Guest

    In <> Irrwahn Grausewitz <> writes:

    > (Dan Pop) wrote:
    >>In <> Eric Sosman <> writes:
    >>
    >>>j0mbolar wrote:
    >>>> say you have char buf[] = "string1 string2 string3";
    >>>>
    >>>> then you want to use sscanf to match "string3" and store

    >> ^^^^^^^^
    >>>> it into another array.

    >...
    >>> If I understand your intent correctly (not certain):
    >>>
    >>> if (sscanf(buf, "%*s%*s%99s", array) == 1)
    >>> ...

    >>
    >>Could you please explain how is this code checking the presence of
    >>"string3" in buf? I suppose this what the OP meant by "to match".

    >
    >Er... well, maybe, but it doesn't make much sense to look for a
    >literal occurrence of a certain substring *and then copy that to
    >another array*, does it?!?


    Not as a *complete* programming problem, but it is perfectly sensible
    as *part* of a programming problem, where the contents of the other
    array may depend on a sequence of tests:

    - if the pattern match succeeds, it is the pattern
    - else if ..., it is ...
    - else it is a default value

    >>Unless I'm missing something obvious, the OP wasn't asking about how to
    >>extract the third word in buf.

    >
    >As I understood, the OP was asking exactly about that: how to
    >extract the last of three blank separated words from a given
    >string, but used the word 'match' in a less strict sense than
    >you inferred. I may still be mistaken, though.


    The usage of the word "regex" in the subject line is a very strong
    clue that he used the word "match" as in a regular expression context.

    Otherwise, the straightforward problem description would have been:
    "how can I use sscanf to extract the third word in a string?", but
    I strongly suspect that the OP could figure out the answer by himself.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jun 18, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anonymous
    Replies:
    0
    Views:
    486
    Anonymous
    Oct 30, 2003
  2. pramod
    Replies:
    22
    Views:
    1,848
    Lew Pitcher
    Jan 6, 2004
  3. Mike

    sscanf and c++!

    Mike, Jun 8, 2004, in forum: C++
    Replies:
    3
    Views:
    3,557
  4. Matt

    sscanf feature in C++?

    Matt, Oct 18, 2004, in forum: C++
    Replies:
    15
    Views:
    19,027
    Default User
    Oct 19, 2004
  5. Replies:
    3
    Views:
    770
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page