Note that there are some explanatory texts on larger screens.

plurals
  1. POextract text between delimiters in c
    primarykey
    data
    text
    <p>What I'm looking for is basically equivalent to extracting the text between parentheses, and that's the example I'll use. If my input is something like <code>(test 1 2)(test 3 4)test foo bar(test again)</code> the code below returns exactly what I want:</p> <pre><code>token: test 1 2 token: test 3 4 token: test again </code></pre> <p>But if my input is <code>(test 1 (test 2 3)foo(bar test) again)</code> I get</p> <pre><code>token: test 1 token: test 2 3 token: bar test </code></pre> <p>I don't want to see <code>test 1</code>. Yes, it's between a left paren and right paren, but it means something went wrong further upstream from this bit of data.</p> <pre><code>void print_tokens(char *s) { printf("input: %s\n",s); char *output; const char *valid = "abcdefghijklmnopqrstuvwxyz0123456789 "; unsigned int length; s=strchr(s,'(')+1; length=strspn(s,valid); while(s!=NULL &amp;&amp; length&gt;0) { output=malloc(length+1); strncpy(output,s,length); output[length]='\0'; printf("token: %s\n",output); free(output); if(strchr(s,'(')!=NULL) { s=strchr(s,'(')+1; length=strspn(s,valid); } else { s=NULL; } } } </code></pre> <p>Aside from being rather loose with the memory management, this slap-dash approach might be "close enough" as I have another function (to which the output will actually get passed that does some checking on its own input, but this could still let some malformed expressions through, and I'd like to avoid that if possible. Though what I'm looking for could be easily summarized in a regular expression (<code>/.*\(([a-z0-9 ]*?)\)/</code> I believe), it has to be in more-or-less standard C. GNU C, Boost, and most of the other libraries I've seen that help with string parsing or regexes aren't available to me.</p> <p>Is there some better way to pull out this text that will eliminate the extraneous token? And be more flexible with the valid character set (for instance, everything <em>except</em> left and right parentheses)?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload