sscanf regex

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • j0mbolar

    #1

    sscanf regex

    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);
  • Eric Sosman

    #2
    Re: sscanf regex

    j0mbolar wrote:[color=blue]
    > 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);[/color]

    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@sun .com

    Comment

    • Arthur J. O'Dwyer

      #3
      Re: sscanf regex


      On Wed, 16 Jun 2004, j0mbolar wrote:[color=blue]
      >
      > 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);[/color]

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

      would be the simplest, I think.

      -Arthur

      Comment

      • Irrwahn Grausewitz

        #4
        Re: sscanf regex

        j0mbolar@engine er.com (j0mbolar) wrote:[color=blue]
        >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);[/color]

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


        Regards
        --
        Irrwahn Grausewitz (irrwahn33@free net.de)
        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

        Comment

        • Dan Pop

          #5
          Re: sscanf regex

          In <2d31a9f9.04061 61255.5f9b0fd1@ posting.google. com> j0mbolar@engine er.com (j0mbolar) writes:
          [color=blue]
          >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);[/color]

          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@ifh.de

          Comment

          • Dan Pop

            #6
            Re: sscanf regex

            In <40D0BC4F.50307 04@sun.com> Eric Sosman <Eric.Sosman@su n.com> writes:
            [color=blue]
            >j0mbolar wrote:[color=green]
            >> say you have char buf[] = "string1 string2 string3";
            >>
            >> then you want to use sscanf to match "string3" and store[/color][/color]
            ^^^^^^^^[color=blue][color=green]
            >> it into another array.
            >>
            >> so:
            >>
            >> char buf[] = "string1 string2 string3";
            >> char array[100];
            >>
            >> sscanf(buf, "what format string should we use?", array);[/color]
            >
            > If I understand your intent correctly (not certain):
            >
            > if (sscanf(buf, "%*s%*s%99s ", array) == 1)
            > ...
            >
            >is what you want. This format[/color]

            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@ifh.de

            Comment

            • Eric Sosman

              #7
              Re: sscanf regex

              Dan Pop wrote:[color=blue]
              > In <40D0BC4F.50307 04@sun.com> Eric Sosman <Eric.Sosman@su n.com> writes:
              >
              >[color=green]
              >>j0mbolar wrote:
              >>[color=darkred]
              >>>say you have char buf[] = "string1 string2 string3";
              >>>
              >>>then you want to use sscanf to match "string3" and store[/color][/color]
              >
              > ^^^^^^^^
              >[color=green][color=darkred]
              >>>it into another array.
              >>>
              >>>so:
              >>>
              >>>char buf[] = "string1 string2 string3";
              >>>char array[100];
              >>>
              >>>sscanf(buf , "what format string should we use?", array);[/color]
              >>
              >> If I understand your intent correctly (not certain):
              >>
              >> if (sscanf(buf, "%*s%*s%99s ", array) == 1)
              >> ...
              >>
              >>is what you want. This format[/color]
              >
              >
              > 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.[/color]

              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@sun .com

              Comment

              • Alex Monjushko

                #8
                Re: sscanf regex

                Eric Sosman <Eric.Sosman@su n.com> wrote:[color=blue]
                > Dan Pop wrote:[/color]

                [snip]
                [color=blue][color=green]
                >> 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.[/color][/color]
                [color=blue]
                > Well, I *said* I wasn't sure I'd understood the O.P.'s
                > intent. I thought about offering[/color]
                [color=blue]
                > if (strchr(buf, "string3") != NULL)[/color]
                ^^^^^^[color=blue]
                > strcpy(array, "string3");[/color]

                I think you mean 'strstr'.

                --
                Alex Monjushko (monjushko@hotm ail.com)

                Comment

                • Irrwahn Grausewitz

                  #9
                  Re: sscanf regex

                  Dan.Pop@cern.ch (Dan Pop) wrote:[color=blue]
                  >In <40D0BC4F.50307 04@sun.com> Eric Sosman <Eric.Sosman@su n.com> writes:
                  >[color=green]
                  >>j0mbolar wrote:[color=darkred]
                  >>> say you have char buf[] = "string1 string2 string3";
                  >>>
                  >>> then you want to use sscanf to match "string3" and store[/color][/color]
                  > ^^^^^^^^[color=green][color=darkred]
                  >>> it into another array.[/color][/color][/color]
                  ....[color=blue][color=green]
                  >> If I understand your intent correctly (not certain):
                  >>
                  >> if (sscanf(buf, "%*s%*s%99s ", array) == 1)
                  >> ...[/color]
                  >
                  >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".[/color]

                  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?!?
                  [color=blue]
                  >Unless I'm missing something obvious, the OP wasn't asking about how to
                  >extract the third word in buf.[/color]

                  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 (irrwahn33@free net.de)
                  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

                  Comment

                  • Dan Pop

                    #10
                    Re: sscanf regex

                    In <40D1C00A.70008 08@sun.com> Eric Sosman <Eric.Sosman@su n.com> writes:
                    [color=blue]
                    >Dan Pop wrote:[color=green]
                    >> In <40D0BC4F.50307 04@sun.com> Eric Sosman <Eric.Sosman@su n.com> writes:
                    >>
                    >>[color=darkred]
                    >>>j0mbolar wrote:
                    >>>
                    >>>>say you have char buf[] = "string1 string2 string3";
                    >>>>
                    >>>>then you want to use sscanf to match "string3" and store[/color]
                    >>
                    >> ^^^^^^^^
                    >>[color=darkred]
                    >>>>it into another array.
                    >>>>
                    >>>>so:
                    >>>>
                    >>>>char buf[] = "string1 string2 string3";
                    >>>>char array[100];
                    >>>>
                    >>>>sscanf(bu f, "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[/color]
                    >>
                    >>
                    >> 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.[/color]
                    >
                    > 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,[/color]

                    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.
                    [color=blue]
                    >so I tried to look for another
                    >interpretati on of an imprecise problem statement.[/color]

                    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@ifh.de

                    Comment

                    • Dan Pop

                      #11
                      Re: sscanf regex

                      In <63j3d091hkp5u7 fr375h1kaa666p6 n91r6@4ax.com> Irrwahn Grausewitz <irrwahn33@free net.de> writes:
                      [color=blue]
                      >Dan.Pop@cern.c h (Dan Pop) wrote:[color=green]
                      >>In <40D0BC4F.50307 04@sun.com> Eric Sosman <Eric.Sosman@su n.com> writes:
                      >>[color=darkred]
                      >>>j0mbolar wrote:
                      >>>> say you have char buf[] = "string1 string2 string3";
                      >>>>
                      >>>> then you want to use sscanf to match "string3" and store[/color]
                      >> ^^^^^^^^[color=darkred]
                      >>>> it into another array.[/color][/color]
                      >...[color=green][color=darkred]
                      >>> If I understand your intent correctly (not certain):
                      >>>
                      >>> if (sscanf(buf, "%*s%*s%99s ", array) == 1)
                      >>> ...[/color]
                      >>
                      >>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".[/color]
                      >
                      >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?!?[/color]

                      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
                      [color=blue][color=green]
                      >>Unless I'm missing something obvious, the OP wasn't asking about how to
                      >>extract the third word in buf.[/color]
                      >
                      >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.[/color]

                      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@ifh.de

                      Comment

                      Working...