Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sscanf not extracting pattern

Tags:

c

regex

scanf

I am trying to figure out the pattern I should be giving to sscanf. I have a string abcde(1GB). I want to extract 1 and GB. I am using

    char list[]= "abcde(1GB)";
    int memory_size =0;
    char unit[3]={0} ;
sscanf(list, "%*s%d%s" , &memory_size, unit);

I do not see tokens extracted when I print I see memory_size =0 and NULL in unit.

Thanks

like image 587
user2737926 Avatar asked Mar 31 '26 16:03

user2737926


1 Answers

your sscanf() string format should be:

sscanf(list, "%*[^(](%d%[^)]" , &memory_size, unit);
  • %[^)] means catch charachters and stop ctaching when finding the charachter ) or end of the string
  • %*[^(] means:
    • [^\(] means catch charachters and stop ctaching when finding the charachter ( - as opposed to a more conventional %s - catching charachters and stop ctaching when finding space characters"
    • * means "read but not store"
like image 184
MOHAMED Avatar answered Apr 02 '26 14:04

MOHAMED



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!